如何使用聚合检查 MongoDB 数据库中的所有文档以获取特定字段的最新值?
How to check all the documents in a MongoDB database for the latest value of a particular field using aggregation?
我是 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_id
的最大值 book_version
以及整个对象:
{
"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”
}
您可以$group by book_id
and capture lastVersion using $max operator. Then you need to $filter all the books belonging to particular group and use $replaceRoot将找到的单个最新版本提升到根级别。
db.collection.aggregate([
{
$group: {
_id: "$book_id",
lastVersion: { $max: "$book_version" },
versions: { $push: "$$ROOT" }
}
},
{
$replaceRoot: {
newRoot: {
$let: {
vars: {
lastVer: {
$filter: {
input: "$versions",
cond: { $eq: [ "$$this.book_version", "$lastVersion" ] }
}
}
},
in: { $arrayElemAt: [ "$$lastVer", 0 ] }
}
}
}
}
])
您可以使用 $sort
and $group
的组合来达到想要的结果:
db.collection.aggregate([
{
$sort: {
// Sort by the book_version from highest to lowest
book_version: -1
}
},
{
$group: {
// Group the documents by their book_id
_id: "$book_id",
book_version: {
// The first book_version is the highest book version
// The same thing applies to every other first-of other book fields
$first: "$book_version"
},
book_id: {
$first: "$book_id"
},
author: {
"$first": "$author"
},
name: {
"$first": "$name"
},
comments: {
"$first": "$comments"
}
}
}
])
我是 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_id
的最大值 book_version
以及整个对象:
{
"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”
}
您可以$group by book_id
and capture lastVersion using $max operator. Then you need to $filter all the books belonging to particular group and use $replaceRoot将找到的单个最新版本提升到根级别。
db.collection.aggregate([
{
$group: {
_id: "$book_id",
lastVersion: { $max: "$book_version" },
versions: { $push: "$$ROOT" }
}
},
{
$replaceRoot: {
newRoot: {
$let: {
vars: {
lastVer: {
$filter: {
input: "$versions",
cond: { $eq: [ "$$this.book_version", "$lastVersion" ] }
}
}
},
in: { $arrayElemAt: [ "$$lastVer", 0 ] }
}
}
}
}
])
您可以使用 $sort
and $group
的组合来达到想要的结果:
db.collection.aggregate([
{
$sort: {
// Sort by the book_version from highest to lowest
book_version: -1
}
},
{
$group: {
// Group the documents by their book_id
_id: "$book_id",
book_version: {
// The first book_version is the highest book version
// The same thing applies to every other first-of other book fields
$first: "$book_version"
},
book_id: {
$first: "$book_id"
},
author: {
"$first": "$author"
},
name: {
"$first": "$name"
},
comments: {
"$first": "$comments"
}
}
}
])