Details
-
Improvement
-
Resolution: Unresolved
-
Minor - P4
-
None
-
None
-
None
-
Query Optimization
-
Query 2020-09-21, Query 2020-10-05
Description
When reading queries with $in, I find it more appropriate to mentally replace the $in operator with $any. Compare the following two queries which could cause confusion for the developer and lead to silent application bugs:
db.inventory.find({ tags: { $in: ["red", "blue" ] } }) |
db.inventory.find({ tags: { $all: ["red", "blue" ] } }) |
Note that even when reading an $all query, the developer might use the word "in". For example, I read the $all query above as: "documents whose tags array includes all items in the given array".
However, replacing $in with $any, I would read the query as "documents whose tags array includes any item in the given array". This matches the behavior of the $in operator more accurately than the in word "in" the $in operator itself. In fact, the documentation for the $in operator uses the word "any" along with "in" to phrase the description of $in:
The $in operator selects the documents where the value of a field equals any value in the specified array.
Also in the queries Query and Projection Operators documentation:
Matches any of the values specified in an array.
While $in does make sense on its own, it is easy to confuse the behavior of $in and $all with the current terminology. An alias like $any would clarify the confusion and obviate many bugs and unnecessary printf debugging for the developers.
Attachments
Issue Links
- related to
-
SERVER-44355 Add $some array query operator
-
- Backlog
-
-
DOCS-13916 Describe $in in the article for querying arrays
-
- Closed
-