Details
-
Improvement
-
Resolution: Done
-
Minor - P4
-
None
-
None
Description
This is being created due to the linked ticket. I feel the problem is common (and simple) enough to warrant a small docs update.
People will sometimes want to literally translate the SQL clause:
WHERE a = 5 OR a = 6
|
into
{$or: [{a: 5}, {a: 6}]}
|
instead of using the `$in` operator. Furthermore they'll follow the same logic to translate:
WHERE (a = 5 OR a = 6) AND (b = 7 OR b = 8)
|
into
{$and: [{$or: [{a: 5}, {a: 6}]},
|
{$or: [{b: 7}, {b: 8}]}]}
|
because they know queries are (generally implemented as) a map and there can't be two distinct values for the same key.
Using the `$in` operator really shines in this case to simplify the query to:
{a: {$in: [5, 6]},
|
b: {$in: [7, 8]}}
|
Specifically speaking, the `$in` operator should be used as a special case of `$or` where the key being checked is always the same.
Speaking more generally, there may be enough oddities trying to translate directly from SQL to Mongo's query language that warrants a quick page of tips for those with an existing strong SQL querying experience.
Attachments
Issue Links
- related to
-
DOCS-88 Logical Operators in Queries
-
- Closed
-