mongodb order / $ sort by array entry

时间:2014-04-18 17:02:30

标签: javascript node.js mongodb mongoose

项目架构:

var ItemSchema = new Schema({
   name: {type : String},
   possibleorders: [{
       name: {type : String},
       value: {type : Number}
   }]
})

如何通过具有特定名称的可能订单的值来订购/ $对项目进行排序?

1 个答案:

答案 0 :(得分:1)

您可以尝试使用聚合框架。

db.collectionName.aggregate([
  {$unwind: "$possibleorders"}, 
  {$sort: {"possibleorders.name":1}}, 
  {$group: {_id:"$_id",name:"$name", possibleorders: {$push:"$possibleorders"}}}
]);