我在mongoDB上的聚合管道就像。
struct ABLoop {
float a, b, time_from_a;
};
我的资产集合中有条目,如。
db.assets.aggregate([{"$match":{"$or":[{"albums":{"$elemMatch":{"id":"78c72b85944e5085a4a3be77a2d175fe","order":{"$gt":"C2"}}}},{"albums":{"$elemMatch":{"id":"18f292072b7555965e0f61a0331a3a43","order":{"$gt":"C2"}}}}]}}
,{"$unwind":"$albums"},
{"$sort":{"albums.order":1,"created":1}},
{"$group":{"_id":"$albums.id","assets":{"$push":"$$ROOT"}}},
{"$project":{"assets":{"$slice":["$assets",6]}}}])
返回的结果类似于。
{
"_id": "8ed6dc473e331d895ecbdea7f9bbd55e",
"created": 1479463428837247,
"updated": 1479463428837247,
"catalog_id": "4b5ce056175151e3f0aa1741eedb7f12",
"albums": {
"id": "18f292072b7555965e0f61a0331a3a43",
"order": "D1"
}
},
{
"_id": "aec701d23dfe5f3a100d678e1a04f716",
"created": 1479463428902155,
"updated": 1479463428902155,
"catalog_id": "4b5ce056175151e3f0aa1741eedb7f12",
"albums": {
"id": "18f292072b7555965e0f61a0331a3a43",
"order": "D2"
}
},
{
"_id": "b2fe8f9dcf8f3f92f6396cfa574ef71c",
"created": 1479463428971650,
"updated": 1479463428971650,
"catalog_id": "4b5ce056175151e3f0aa1741eedb7f12",
"albums": {
"id": "18f292072b7555965e0f61a0331a3a43",
"order": "E1"
}
},
{
"_id": "050b550fb23bf780f93eec43cbc667f1",
"created": 1479463428065297,
"updated": 1479463428065297,
"catalog_id": "4b5ce056175151e3f0aa1741eedb7f12",
"albums": {
"id": "78c72b85944e5085a4a3be77a2d175fe",
"order": "D1"
}
},
{
"_id": "dc54279bc0318f41808b65d1fe7142b2",
"created": 1479463428134560,
"updated": 1479463428134560,
"catalog_id": "4b5ce056175151e3f0aa1741eedb7f12",
"albums": {
"id": "78c72b85944e5085a4a3be77a2d175fe",
"order": "D2"
}
}
结果与用于或在匹配查询中传递的相册的顺序不同。 78c72b85944e5085a4a3be77a2d175fe将在18f292072b7555965e0f61a0331a3a43之后发布。理想情况下,它应该与传递给查询的顺序相同。任何想法
答案 0 :(得分:0)
我知道这真的很晚,但是我发现自己进行了查找,并经历了大约25个堆栈溢出问题,因此我认为应该发表。这是我所做的(用于聊天室,因此,如果您想分页,请按相反的顺序进行。
Group.find({ name: { $regex : new RegExp(name, "i") } })
.skip(pageOptions.page*pageOptions.limit)
.limit(pageOptions.limit)
.sort( '-createdAt' )
.populate("sender")
.exec((err, result) => {
result.reverse();
callback(err, result);
});
关键是:
result.reverse();
希望这可以帮助某人。