sorting array in object in mongodb is not working [duplicate]
An option is to run an aggregation that:
- unwinds the array
- sorts by
friends.name
- regroups
The pipeline should look something like this:
db.collection.aggregate([
{ $match: { name: 'dan' }},
{ $unwind: '$friends' },
{ $sort: { 'friends.height': -1 }},
{ $group: { _id: '$name', friends: { $push: '$friends'}}])