MongoDB order type

I have this collection in MongoDB. It contains values ​​of different types under the key val.
Also, note that I sort it in ascending order of val.

[test] 2014-02-20 08:53:11.857 >>> db.account.find().sort({val:1}); { "_id" : ObjectId("5304d25786dd4b348bcc2b2e"), "username" : "usr10", "password" : "123", "val" : [ ] } { "_id" : ObjectId("5304d29986dd4b348bcc2b2f"), "username" : "usr20", "password" : "456", "val" : null } { "_id" : ObjectId("5304e31686dd4b348bcc2b37"), "username" : "usr80", "password" : "555", "val" : 1 } { "_id" : ObjectId("5304d50a86dd4b348bcc2b32"), "username" : "usr50", "password" : "555", "val" : [ 40 ] } { "_id" : ObjectId("5304d4c886dd4b348bcc2b31"), "username" : "usr40", "password" : "777", "val" : 200 } { "_id" : ObjectId("5304d2a186dd4b348bcc2b30"), "username" : "usr30", "password" : "888", "val" : { } } { "_id" : ObjectId("5304d97786dd4b348bcc2b33"), "username" : "usr50", "password" : "555", "val" : { "ok" : 1 } } { "_id" : ObjectId("5304e2dc86dd4b348bcc2b36"), "username" : "usr80", "password" : "555", "val" : true } { "_id" : ObjectId("5304e22f86dd4b348bcc2b34"), "username" : "usr60", "password" : "555", "val" : ISODate("2014-02-19T16:56:15.787Z") } { "_id" : ObjectId("5304e2c786dd4b348bcc2b35"), "username" : "usr70", "password" : "555", "val" : /abc/ } [test] 2014-02-20 08:53:19.357 >>> 

I am reading a book that says the following.

MongoDB has a hierarchy regarding how types are mapped. Sometimes you will have one key with several types: for example, integers and booleans, or strings and nulls. If you do a sort by key with a mixed type, there is a predefined version for them to be sorted. Smallest to largest this order
is as follows:
1. The minimum value
2.null
3. Numbers (integers, long, double)
4. Rows
5. Object / document
6. Array
7. Binary data
8. Object identifier
9. Logical
10. Date
11. Timestamp
12. Regular expression
13. Maximum value

So why is my sort order different? For example, photos when I sort (see above), I see these strange things:

1) I have no idea what the "minimum value" and "maximum value" mean.

2) An array comes before a number. And an empty array comes before zero.

3) Number 1 in front of the array

4) Array [40] is between numbers 1 and 200.

Can someone just explain this result in some detail?
Thank you very much in advance.

+6
source share
3 answers

Your book says the same as the official documentation. But this also does not explain the obscure sort order of the two arrays. At least two types of Minimum value and Maximum value explained. They are internal.

+2
source

Type ordering is used only when there is no other supported way to arrange items. Array fields have their own sorting behavior, where the minimum value of their elements is used in ascending sorting and the maximum value in downward sorting. The type of this minimum or maximum value is then used to order documents with fields of this type.

So [40] appears after 1 , but up to 200 , because the minimum value of this array is 40 .

An empty array does not matter at all, so it ends with a document where null . If I cancel the sort, they will remain in the same order, which means that MongoDB considers them equal.

+2
source

Where is the sort clause in your request? Your sort order appears by default - pay attention to the upstream ObjectIds. You mentioned that you sort by val , so I expect your request to be

db.account.find({val:1})

0
source

All Articles