[DOCS-382] Explain in $or query operation documentation when using $in might be optimal Created: 07/Aug/12  Updated: 30/Oct/23  Resolved: 27/Sep/12

Status: Closed
Project: Documentation
Component/s: manual
Affects Version/s: None
Fix Version/s: Server_Docs_20231030

Type: Improvement Priority: Minor - P4
Reporter: Daniel Gottlieb (Inactive) Assignee: Kay Kim (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to DOCS-88 Logical Operators in Queries Closed
Participants:
Days since reply: 11 years, 22 weeks, 6 days ago

 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.



 Comments   
Comment by auto [ 13/Sep/12 ]

Author:

{u'date': u'2012-09-13T07:21:36-07:00', u'email': u'kay.kim@10gen.com', u'name': u'kay'}

Message: DOCS-484, DOCS-382, DOCS-281 Update operators ref docs and redirect compact command
Branch: master
https://github.com/mongodb/docs/commit/52769b38cd3f6bb53da25529333dec50610b04eb

Comment by auto [ 13/Sep/12 ]

Author:

{u'date': u'2012-09-10T19:39:57-07:00', u'email': u'kay.kim@10gen.com', u'name': u'kay'}

Message: DOCS-484, DOCS-382 Update of the operators reference documentation
Branch: master
https://github.com/mongodb/docs/commit/8950a132ae7fbccfd81fc50a6c189906d7ddcfdd

Comment by auto [ 13/Sep/12 ]

Author:

{u'date': u'2012-09-10T14:09:56-07:00', u'email': u'kay.kim@10gen.com', u'name': u'kay'}

Message: DOCS-484, DOCS-382 Update of the reference operators
Branch: master
https://github.com/mongodb/docs/commit/b25eecc19c81249e5bb190173ce7696f1dda238e

Comment by Daniel Gottlieb (Inactive) [ 07/Aug/12 ]

Maybe it's really a duplicated case, but DOCS-88 is a bit light on details/examples on exactly what it wants to accomplish.

Generated at Thu Feb 08 07:38:34 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.