0

db.information is the collection that contains the following structure:

information:
           {
               class_num:
                { 
                     standard:
                     [
                           {
                             name:tom,
                             weight:30
                           },
                           {
                             name:mike,
                             weight:32
                           },
                           {
                             name:sally,
                             weight:25
                           }
                      ]
                }
            }

I want to sort this data by weight, I tried to use find().sort('weight':-1) but it doesn't work. How should I point the sort towards the array inside array?

Vijay Rajpurohit
  • 1,266
  • 2
  • 13
  • 25
mylamo
  • 13
  • 3
  • like this ? https://stackoverflow.com/questions/15388127/mongodb-sort-inner-array – jin Sep 05 '19 at 00:37
  • use: information.class_num.standard.$.1.weight – Dexter Sep 05 '19 at 04:10
  • @jinzy I tried it with just one change, adding {'$match':{ '_id' : ObjectId("5d69c4d11c9d440000792682")}}, as first line along with the other lines mentioned db.servers.aggregate( {$unwind: '$service.apps.updates'}, {$sort: {'service.apps.updates.date': 1}}, {$group: {_id: '$_id', 'updates': {$push: '$service.apps.updates'}}}, {$project: {'service.apps.updates': '$updates'}}), but i am getting an error, The field '5d69c4d11c9d440000792682' must be an accumulator object I am not sure how to proceed – mylamo Sep 05 '19 at 20:14

0 Answers0