Я новичок в MongoDB и работаю с ним над кодом NodeJS.
Мне нужно использовать функции агрегирования, чтобы получить последние book_version
каждой отдельной книги.
Вот какмоя база данных выглядит так:
[
{
"book_id": "ab12nld”,
"book_version": "0”,
"author": “Sam”,
“name”: “Sample Book”,
“comments”: “Done”
},
{
"book_id": "ab12nld”,
"book_version": "1",
"author": "Martin",
"name": "Sample Book",
“comments”: “In Progress”
},
{
"book_id": "ab12nld”,
"book_version": "2",
"author": "Roy",
"name": "Sample Book",
“comments”: “To-Do”
}
]
[
{
"book_id": "bcj123n”,
"book_version": "0”,
"author": “Don”,
“name”: “Another Book”,
“comments”: “Done”
},
{
"book_id": "bcj123n”,
"book_version": "1",
"author": "Ray",
"name": "Another Book",
“comments”: “In Progress”
},
{
"book_id": "bcj123n”,
"book_version": "2",
"author": "Max",
"name": "Another Book",
“comments”: “To-Do”
}
]
[
{
"book_id": "k23d1d3”,
"book_version": "0”,
"author": “Sunny”,
“name”: “New Book”,
“comments”: “Done”
},
{
"book_id": "k23d1d3”,
"book_version": "1",
"author": "Archer",
"name": "New Book",
“comments”: “In Progress”
}
]
[
{
"book_id": "o902f3s”,
"book_version": "0”,
"author": “Joy”,
“name”: “Nice Book”,
“comments”: “Done”
},
{
"book_id": "o902f3s”,
"book_version": "1",
"author": "James",
"name": "Nice Book",
“comments”: “In Progress”
},
{
"book_id": "o902f3s”,
"book_version": "2”,
"author": “Pam”,
“name”: “Nice Book”,
“comments”: “Done”
},
{
"book_id": "o902f3s”,
"book_version": "3",
"author": "Jonah",
"name": "Nice Book",
“comments”: “Completed”
}
]
В результате я должен получить максимальное значение book_version
для каждого book_id
вместе со всем объектом:
{
"book_id": "ab12nld”,
"book_version": "2",
"author": "Roy",
"name": "Sample Book",
“comments”: “To-Do”
},
{
"book_id": "bcj123n”,
"book_version": "2",
"author": "Max",
"name": "Another Book",
“comments”: “To-Do”
},
{
"book_id": "k23d1d3”,
"book_version": "1",
"author": "Archer",
"name": "New Book",
“comments”: “In Progress”
},
{
"book_id": "o902f3s”,
"book_version": "3",
"author": "Jonah",
"name": "Nice Book",
“comments”: “Completed”
}