Mongodb $ in performance with single array element versus $ eq

In our code, for convenience, we use queries such as

db.collection.find({ "field": { $in: array } })

even if it arraycontains only one element. We could rewrite it in this case just be

db.collection.find({ "field": "element" })

We thought that these queries would behave the same, but we noticed that with complex queries that contain statements $orand multiple fields, while explain()showing the same query plan for both cases, the actual execution of the queries quickly returns for a simple case , when used, $inis executed forever, because perhaps it uses different index scans.

Why didn't the mongodb query compiler turn $inwith one element into the same thing as $eq? And why does explain()it still show that they use the same index and select scans, while in fact query execution explicitly uses different plans?

+4
source share
1 answer

Same

use

.explain()

to see the final request

db.collection.find({ "field": { $in: array } }).explain()
db.collection.find({ "field": "element" }).explain()

the $intranslated to $eqif the array contains only one element

+1
source

Source: https://habr.com/ru/post/1673866/


All Articles