sorting array in object in mongodb is not working [duplicate]

An option is to run an aggregation that:

  1. unwinds the array
  2. sorts by friends.name
  3. 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'}}])