我正在尝试对未绑定的子文档进行排序。这是集合:
[
{
otherFields: {},
visites: ["58d46ffbf1c383862cf5de4a", "58d46ffbf1c383862cf5de49"]
}
]
这是聚合:
db.exps.aggregate([
{ "$match": {} },
{ "$project": { "visites": 1 } },
{ "$unwind": "$visites" },
{ "$sort": { "visites.formVersion": 1 } },
{ "$group": { "_id": null, "count": { "$sum": 1 }, "data": { "$push": "$visites" } } }
])
我意识到用子字段排序是没有意义的,因为visites
字段只是一个id的数组。是否可以在聚合中填充它们,以便对它们进行排序?
nodejs人口之后的结果是:
{
"count": 50,
"data": [
{
"_id": "58d46ffbf1c383862cf5de4a",
"formVersion": 4,
"start": "2017-06-04T07:30:00.000Z",
"end": "2017-06-04T08:30:00.000Z",
"type": "assessor",
"expedient": "58d46ffaf1c383862cf5de41",
"user": "58d46ff8f1c383862cf5de3a",
"__v": 0,
"isDeleted": false,
"lastModified": "2017-03-24T01:01:47.343Z",
"created": "2017-03-24T01:01:47.330Z"
},
{
"_id": "58d46ffbf1c383862cf5de49",
"formVersion": 3,
"start": "2017-06-03T07:30:00.000Z",
"end": "2017-06-03T08:30:00.000Z",
"type": "assessor",
"expedient": "58d46ffaf1c383862cf5de41",
"user": "58d46ff8f1c383862cf5de3a",
"__v": 0,
"isDeleted": false,
"lastModified": "2017-03-24T01:01:47.341Z",
"created": "2017-03-24T01:01:47.328Z"
},
答案 0 :(得分:0)
假设有两个集合coll1
和coll2
:
> db.coll1.find()
{
"_id": ObjectId("58eecf2a11b7af6f560666d8"),
"otherFields": {
},
"visites": [
"58d46ffbf1c383862cf5de4a",
"58d46ffbf1c383862cf5de49"
]
}
> db.coll2.find()
{
"_id": "58d46ffbf1c383862cf5de4a",
"formVersion": 4,
"start": "2017-06-04T07:30:00.000Z",
"end": "2017-06-04T08:30:00.000Z",
"type": "assessor",
"expedient": "58d46ffaf1c383862cf5de41",
"user": "58d46ff8f1c383862cf5de3a",
"__v": 0,
"isDeleted": false,
"lastModified": "2017-03-24T01:01:47.343Z",
"created": "2017-03-24T01:01:47.330Z"
}
{
"_id": "58d46ffbf1c383862cf5de49",
"formVersion": 3,
"start": "2017-06-03T07:30:00.000Z",
"end": "2017-06-03T08:30:00.000Z",
"type": "assessor",
"expedient": "58d46ffaf1c383862cf5de41",
"user": "58d46ff8f1c383862cf5de3a",
"__v": 0,
"isDeleted": false,
"lastModified": "2017-03-24T01:01:47.341Z",
"created": "2017-03-24T01:01:47.328Z"
}
如果您希望按以下方式排序: formVersion
字段,一个可能的聚合管道解决方案可能是:
db.coll1.aggregate([
// left outer join from coll2 to coll1
{$lookup:{
from:"coll2",
localField:"visites",
foreignField:"_id",
as:"data"
}},
// unwind the resulting array
{$unwind:"$data"},
// sort the unwinded documents using the formVersion field
{$sort:{"_id":1 ,"data.formVersion": 1}},
// collect the results into an array
{$group:{"_id":null, data:{$push:"$data"}}}
])
管道的结果是:
{
"_id": null,
"data": [
{
"_id": "58d46ffbf1c383862cf5de49",
"formVersion": 3,
"start": "2017-06-03T07:30:00.000Z",
"end": "2017-06-03T08:30:00.000Z",
"type": "assessor",
"expedient": "58d46ffaf1c383862cf5de41",
"user": "58d46ff8f1c383862cf5de3a",
"__v": 0,
"isDeleted": false,
"lastModified": "2017-03-24T01:01:47.341Z",
"created": "2017-03-24T01:01:47.328Z"
},
{
"_id": "58d46ffbf1c383862cf5de4a",
"formVersion": 4,
"start": "2017-06-04T07:30:00.000Z",
"end": "2017-06-04T08:30:00.000Z",
"type": "assessor",
"expedient": "58d46ffaf1c383862cf5de41",
"user": "58d46ff8f1c383862cf5de3a",
"__v": 0,
"isDeleted": false,
"lastModified": "2017-03-24T01:01:47.343Z",
"created": "2017-03-24T01:01:47.330Z"
}
]
}
请注意,自MongoDB 3.4起,$lookup
阶段存在某些限制;例如,两个集合必须位于同一个数据库中,from
参数不能是分片集合。有关详细信息,请参阅https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/
另请注意,最后一个$group
阶段将产生一个文档,该文档将受MongoDB的16 MB文档大小限制的约束。如果您愿意,可以删除$group
阶段并迭代$sort
阶段返回的光标。