Mongo 聚合每组文档的 returns 最后版本
Mongo aggregate that returns last version of each set of documents
给定名为 sampledata 的集合中的以下文档:
sampledata:
{
"link" : "demo-1",
"type" : "type-1",
"version" : 1,
"slug" : "slug-1",
"_id" : ObjectId("550acc38fd15150c290dd489")
}
{
"link" : "demo-2",
"type" : "type-2",
"version" : 1,
"slug" : "slug-2",
"_id" : ObjectId("550acc37fd15150c290dd46a")
}
{
"link" : "demo-3",
"type" : "type-3",
"version" : 1,
"slug" : "slug-3",
"_id" : ObjectId("550acc38fd15150c290dd46e")
}
{
"link" : "demo-3",
"type" : "type-3",
"version" : 2,
"slug" : "slug-3",
"_id" : ObjectId("550b6182dac1909834b7b38d")
}
{
"link" : "demo-3",
"type" : "type-3",
"version" : 3,
"slug" : "slug-3",
"_id" : ObjectId("550b61d3dac1909834b7b38e")
}
我需要一个 returns 每个 "type" 文档的最后一个 "version" 的聚合管道。因此,结果应该是:
{
"link" : "demo-1",
"type" : "type-1",
"version" : 1,
"slug" : "slug-1",
"_id" : ObjectId("550acc38fd15150c290dd489")
}
{
"link" : "demo-2",
"type" : "type-2",
"version" : 1,
"slug" : "slug-2",
"_id" : ObjectId("550acc37fd15150c290dd46a")
}
{
"link" : "demo-3",
"type" : "type-3",
"version" : 3,
"slug" : "slug-3",
"_id" : ObjectId("550b61d3dac1909834b7b38e")
}
以下提供了我想要的大部分内容,但我很难按原样返回所选文档中的其余字段:
db.sampledata.aggregate([{"$group": { "_id": "$slug", lastVersion: {"$last":"$version"} } }])
$last
operator works in the "last" item in a grouping boundary from the supplied key. Typically it is used with $sort
preceeding the following $group
语句。这 "orders" 集合中的文档以所需的方式,以便 "last" 获取预期的内容:
db.sampledata.aggregate([
{ "$sort": { "slug": 1, "version": 1 } },
{ "$group": {
"_id": "$slug",
"link": { "$last": "$link" },
"type": { "$last": "$type" },
"version": { "$last": "$version" },
"id": { "$last": "$_id" }
}}
])
如您所见,如果您想匹配按该顺序 "last" 的整个文档,其目的是对每个字段使用运算符。要完全重建,您需要以下 $project
:
db.sampledata.aggregate([
{ "$sort": { "slug": 1, "version": 1 } },
{ "$group": {
"_id": "$slug",
"link": { "$last": "$link" },
"type": { "$last": "$type" },
"version": { "$last": "$version" },
"id": { "$last": "$_id" }
}},
{ "$project": {
"_id": "$id",
"slug": "$_id",
"link": 1,
"type": 1,
"version": 1
}}
])
给定名为 sampledata 的集合中的以下文档:
sampledata:
{
"link" : "demo-1",
"type" : "type-1",
"version" : 1,
"slug" : "slug-1",
"_id" : ObjectId("550acc38fd15150c290dd489")
}
{
"link" : "demo-2",
"type" : "type-2",
"version" : 1,
"slug" : "slug-2",
"_id" : ObjectId("550acc37fd15150c290dd46a")
}
{
"link" : "demo-3",
"type" : "type-3",
"version" : 1,
"slug" : "slug-3",
"_id" : ObjectId("550acc38fd15150c290dd46e")
}
{
"link" : "demo-3",
"type" : "type-3",
"version" : 2,
"slug" : "slug-3",
"_id" : ObjectId("550b6182dac1909834b7b38d")
}
{
"link" : "demo-3",
"type" : "type-3",
"version" : 3,
"slug" : "slug-3",
"_id" : ObjectId("550b61d3dac1909834b7b38e")
}
我需要一个 returns 每个 "type" 文档的最后一个 "version" 的聚合管道。因此,结果应该是:
{
"link" : "demo-1",
"type" : "type-1",
"version" : 1,
"slug" : "slug-1",
"_id" : ObjectId("550acc38fd15150c290dd489")
}
{
"link" : "demo-2",
"type" : "type-2",
"version" : 1,
"slug" : "slug-2",
"_id" : ObjectId("550acc37fd15150c290dd46a")
}
{
"link" : "demo-3",
"type" : "type-3",
"version" : 3,
"slug" : "slug-3",
"_id" : ObjectId("550b61d3dac1909834b7b38e")
}
以下提供了我想要的大部分内容,但我很难按原样返回所选文档中的其余字段:
db.sampledata.aggregate([{"$group": { "_id": "$slug", lastVersion: {"$last":"$version"} } }])
$last
operator works in the "last" item in a grouping boundary from the supplied key. Typically it is used with $sort
preceeding the following $group
语句。这 "orders" 集合中的文档以所需的方式,以便 "last" 获取预期的内容:
db.sampledata.aggregate([
{ "$sort": { "slug": 1, "version": 1 } },
{ "$group": {
"_id": "$slug",
"link": { "$last": "$link" },
"type": { "$last": "$type" },
"version": { "$last": "$version" },
"id": { "$last": "$_id" }
}}
])
如您所见,如果您想匹配按该顺序 "last" 的整个文档,其目的是对每个字段使用运算符。要完全重建,您需要以下 $project
:
db.sampledata.aggregate([
{ "$sort": { "slug": 1, "version": 1 } },
{ "$group": {
"_id": "$slug",
"link": { "$last": "$link" },
"type": { "$last": "$type" },
"version": { "$last": "$version" },
"id": { "$last": "$_id" }
}},
{ "$project": {
"_id": "$id",
"slug": "$_id",
"link": 1,
"type": 1,
"version": 1
}}
])