-
Type: Bug
-
Resolution: Won't Do
-
Priority: Major - P3
-
Affects Version/s: None
-
Component/s: manual
-
0.2
----------------------------
Original Description
The comparison/sort order documentation (https://docs.mongodb.com/manual/reference/bson-type-comparison-order/#bson-types-comparison-order) provides the following sort order of BSON types:
- MinKey (internal type)
- Null
- Numbers (ints, longs, doubles, decimals)
- Symbol, String
- Object
- Array
- BinData
- ObjectId
- Boolean
- Date
- Timestamp
- Regular Expression
- MaxKey (internal type)
However at least for object and array types the sort order is different:
If create 2 objects:
db.test.insertOne({"key" : { "key" : "value" } }) db.test.insertOne({"key" : [ "value", "value" ]})
And perform sorting via cursor or aggregate the sort order will be different:
db.test.find().sort({"key":1})
{ "_id" : ObjectId("5b7c585ee9aa2bf28b64c184"), "key" : [ "value", "value" ] } { "_id" : ObjectId("5b7c553c431906bbc5028804"), "key" : { "key" : "value" } }
db.test.aggregate([{"$sort":{"key":1}}])
{ "_id" : ObjectId("5b7c585ee9aa2bf28b64c184"), "key" : [ "value", "value" ] } { "_id" : ObjectId("5b7c553c431906bbc5028804"), "key" : { "key" : "value" } }
NB: in Mongo 3.4.10 the output will be different: cursor sort will return object first.
----------------------------