MongoDB: sort nested array of objects

巧了我就是萌 提交于 2019-11-27 14:45:06

问题


I'm looking for a way to sort a nested array of objects. Here's an example:

answers: [
           {name: 'paul', state: 'RU'},
           {name: 'steve', state: 'US'}, 
           {name: 'mike', state: 'DE'}, 
           ...
         ]

Suppose now I want to find all the name, of the answers array, but sorting it in ascendant order, how can I accomplish that?


回答1:


I would store it in the order you want it back out. Or sort it after you pull it out, on the client side.

If neither of those are possible, you can use the aggregation framework:

> db.test.insert({answers: [
...                 {name: 'paul', state: 'RU'},
...                 {name: 'steve', state: 'US'}, 
...                 {name: 'mike', state: 'DE'}]});
> db.test.insert({answers: [
...                 {name: 'paul', state: 'RU'},
...                 {name: 'steve', state: 'US'}, 
...                 {name: 'xavier', state: 'TX'}]});

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

produces:

{
  "result" : [
  {
    "_id" : ObjectId("5053b2477d820880c3469364"),
    "answers" : [
      {
        "name" : "paul",
        "state" : "RU"
      },
      {
        "name" : "steve",
        "state" : "US"
      },
      {
        "name" : "xavier",
        "state" : "TX"
      }
    ]
  },
  {
    "_id" : ObjectId("5053af9f7d820880c3469363"),
    "answers" : [
      {
        "name" : "mike",
        "state" : "DE"
      },
      {
        "name" : "paul",
        "state" : "RU"
      },
      {
        "name" : "steve",
        "state" : "US"
      }
    ]
  }
],
  "ok" : 1
}



回答2:


This might not be helpful in this context, but I thought I'd add this. You also have the option to sort it on the write which tends to be better for denormalized collections where you don't have to sort in more than one way.

I found this situation in my app when creating a feed on a user.

Meteor.users.helpers({
  'addToFeed': function (gameId, pushData) {
    check(pushData, FeedSchema);
    Meteor.users.update({
      _id: this._id,
      "games.gameId": gameId
    }, {
      $push: {
        "games.$.feed": {
          $each: [pushData],
          $sort: { timestamp: -1 }
        }
      }
    });
  }
});

I found it to be pretty handy because you can then use find() and it will be sorted by your specifications by default.




回答3:


After performing a find() you can use sort() on the return value.

db.collection.find({},{"answers.name":1}).sort({"answers.name":1})

The find would extract the name fields of all documents in the collection. The sort would then sort them by name, ascending.

http://www.mongodb.org/display/DOCS/Sorting+and+Natural+Order



来源:https://stackoverflow.com/questions/12432727/mongodb-sort-nested-array-of-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!