sort by embedded object value in Mongodb
问题 I have a schema like this: { tags:[ { id:"t1", score:70 }, { id:"t1", score:60 } ] } I want to sort the query on tag.id search to get sorted by the corresponding score. So if I search db.collection.find({tags.id:"t1"}).sort({tags.score:-1}) , it sorted by score of the "t1" object not other tags. Any suggestion? 回答1: If you need to calculate out something like this at runtime, with "filtered" content from the array determining the sort order, then you best do something with .aggregate() to