Why is this the behavior in MongoDB?
While BSON is JSON-like, there are notable differences:
- A BSON data structure is an ordered object, not a dictionary. For example, BSON does not require field names to be unique (although drivers commonly default to a hash / dictionary / JSON-like interface that does not support duplicate field names). This gives developers precision which can be important for some use cases and also avoids potentially unnecessary server overhead of inspecting and recursively serializing/deserializing BSON into a predefined field order.
- Where order is important, officially supported drivers rely on the underlying programming language to support an order-preserving data structure or provide their own. For example, the Python driver (aka PyMongo) includes a SON class for manipulating ordered objects similar to a normal Python dictionary.
Number) which represents all values as a double precision floating point number.
- MongoDB has defined comparison and sort order rules for BSON values. For example, MongoDB uses simple binary comparison for strings by default, and MongoDB 3.4+ adds the option of language-specific collation.
The query example you provided is for an exact match on an embedded document, which includes the field order because the underlying data is ordered in BSON. This query performs a binary comparison of the BSON serialization of the embedded document provided in your query against the BSON field value with the embedded document stored in MongoDB. You can potentially take advantage of the field order to consistently put a more selective field earlier in your embedded document, which can help with the query performance if you've indexed the entire embedded document.
If field order is unimportant you should query matching the embedded fields instead, eg:
In this case you would create a compound index to support your common queries. The selectivity, order, and sort direction of keys in the index definition would be significant if you want efficient indexes to support your queries.