我的数据如下:
db.getCollection('users').find({"name" : "dan"}, {"friends" : 1}).sort({"friends.height" : -1})
我想检索朋友按高度降序排序的文档。
我尝试<i>html</i>
<strong>html</strong>
<em>html</em>
<b>html</b>
,但朋友列表保持不变。
答案 0 :(得分:3)
选项是运行以下聚合:
friends.name
管道应如下所示:
db.collection.aggregate([
{ $match: { name: 'dan' }},
{ $unwind: '$friends' },
{ $sort: { 'friends.height': -1 }},
{ $group: { _id: '$name', friends: { $push: '$friends'}}])