Reputation: 1160
for instance, if i'm sorting a collection by val in :
`[{id: 1, val: 1}, {id: 2, val: 2} , {id: 3, val: 3}, {id: 4, val: 2}]
, etc.. How does it determine which val of 2 comes first?
Upvotes: 2
Views: 864
Reputation: 3421
If the query is reading the values sorted from an index, the ordering will most likely be inherited from how the documents are stored in the index (https://docs.mongodb.com/manual/reference/method/cursor.sort/#sort-and-index-use).
For example, if the query is reading the results pre-sorted from the index { "val" : 1, "val2" : 1 }
, then the results would likely be sorted by the field "val2".
If documents are being sorted in memory, then results are likely not guaranteed to have a specific order beyond what is specified in the sort clause.
Upvotes: 2