问题
This specifc question is in relation to using mongodb with the golang package mongo-driver, but I would assume this applies across most interfaces with mongodb.
When using Find to query some data from a collection, we can use both the bson.M- and bson.D-type to specify the filter for this find.
As per the documentation bson.D
should be used if the order of elements matters and bson.M
should be used otherwise.
D is an ordered representation of a BSON document. This type should be used when the order of the elements matters, such as MongoDB command documents. If the order of the elements does not matter, an M should be used instead.
Now my question is whether the use of either of these, i.e. ordered vs unordered, structures has an effect on the query plan generated by the mongo query optimizer.
In a classic SQL database the order usually doesn't matter as the optimizer is smart enough to use summary statistics, indexes, etc. to determine which queries to execute first.
Can I assume this to be the case here as well, or does using an ordered structure to query my collection somehow interfere with this / does using an ordered structure work similar to using optimizer hints? If there is some interference, is this influenced at all by if the fields to search through are indexed?
回答1:
You may use bson.M
for the filter, it usually results in shorter and clearer filter declaration, the order of fields doesn't matter, the MongoDB server is smart enough to find matching indices regardless of the used order. E.g. if you have a compound index with fields A
and B
, using a bson.D
filter listing B
first then A
will not prevent the server to use the existing index. So in this case you may use bson.M
and bson.D
, it doesn't matter.
The order does matter when you specify sort fields for example. It does matter if you sort by field A
then by field B
, it may be a completely different order than sorting by B
first and then by A
. So when you specify a sort document having multiple fields, you should definitely use bson.D
.
The order may also matter when you insert a new document for example. If you use a bson.M
as the document, the order of fields is not guaranteed to be the same in all your documents. When you use bson.D
, then the order in the saved document will match the order as you list the fields in bson.D
.
回答2:
The key order applies to wire protocol, not query execution.
In MongoDB, BSON documents are ordered lists of key-value pairs.
BSON is used for serializing documents stored in collections as well as "command documents", which are the commands sent to the server.
When a server processes a command, it requires the command name to be the first key (remember keys are ordered). The purpose of this is to make command dispatching more efficient.
Once the command is dispatched, the order of keys in the rest of the command document, or in the payload (which includes the find condition in case of find), doesn't matter.
来源:https://stackoverflow.com/questions/64281675/bson-d-vs-bson-m-for-find-queries