[
{
"t": "Sample Question",
"d": "2018-12-23T21:21:03.430Z",
"aa": [
{
"q": 1,
"n": "5bee73accda38",
"iU": "5bee73accda38/5c1f3d7341977/1.mp4"
},
{
"q": 4,
"n": "5bee73accda38",
"iU": "5bee73accda38/5c1f3d7341977/1.mp4"
}
]
}]
我刚接触Mongo,尝试在MongoDB中对SubArray(降序)进行排序,但仍然无法在子数组中实现基于"q“(降序)索引的排序。
这就是我所尝试的,但没有成功。
Question.find({}, function(err, question) {
if (err)
res.send(err);
}).sort({'d':-1,'aa.q':-1 }).skip(page).limit(10);
发布于 2018-12-24 07:13:48
您始终可以在$unwind
中使用$aggregate
,先使用$sort
,然后使用$group
db.collection.aggregate([
{ $unwind: "$aa" },
{ $sort: { "aa.q": -1 }},
{ $group: {
_id: "$_id",
"t": { $first: "$t" },
"d": { $first: "$d" },
"aa": { $push: "$aa" }
}
}
])
https://stackoverflow.com/questions/53907547
复制相似问题