Hide
Consider the collection "things" for demonstration purposes, with 3 documents:
db.things.insert({"_id" : 'a', arr: [{round:1, points:2000}, {round:2, points:1000}, {round:3, points:1000}]});
|
db.things.insert({"_id" : 'b', arr: [{round:1, points:1500}, {round:2, points:1500}, {round:3, points:3000}]});
|
db.things.insert({"_id" : 'c', arr: [{round:1, points:1000}, {round:2, points:2000}, {round:3, points:2000}]});
|
The query:
db.things.find({"arr.round" : 1}).sort({"arr.points" : -1 })
|
returns the 3 documents in the following order:
b, a, c
However, if this index is created
db.things.createIndex({"arr.round" : 1, "arr.points" : -1});
|
The same query returns the documents in a different order (if index is used):
a, b, c
Nevertheless, If we want to always replicate this (second) sort behaviour with or without index, it can be done like this:
db.things.find({"arr":{"$elemMatch" : {round : 1}}}).sort({"arr.$.points" : -1 })
|
Show
Consider the collection "things" for demonstration purposes, with 3 documents:
db.things.insert({"_id" : 'a', arr: [{round:1, points:2000}, {round:2, points:1000}, {round:3, points:1000}]});
db.things.insert({"_id" : 'b', arr: [{round:1, points:1500}, {round:2, points:1500}, {round:3, points:3000}]});
db.things.insert({"_id" : 'c', arr: [{round:1, points:1000}, {round:2, points:2000}, {round:3, points:2000}]});
The query:
db.things.find({"arr.round" : 1}).sort({"arr.points" : -1 })
returns the 3 documents in the following order:
b, a, c
However, if this index is created
db.things.createIndex({"arr.round" : 1, "arr.points" : -1});
The same query returns the documents in a different order (if index is used):
a, b, c
Nevertheless, If we want to always replicate this (second) sort behaviour with or without index, it can be done like this:
db.things.find({"arr":{"$elemMatch" : {round : 1}}}).sort({"arr.$.points" : -1 })