|
19 | 19 | *at least* one of the ``<expressions>``.
|
20 | 20 |
|
21 | 21 | Consider the following query:
|
22 |
| - |
| 22 | + |
23 | 23 | .. code-block:: javascript
|
24 |
| - |
| 24 | + |
25 | 25 | db.inventory.find( { price:1.99, $or: [ { qty: { $lt: 20 } }, { sale: true } ] } )
|
26 |
| - |
| 26 | + |
27 | 27 | This query will select all documents in the ``inventory`` collection
|
28 | 28 | where:
|
29 |
| - |
30 |
| - - the ``price`` field value equals ``1.99`` *and* |
| 29 | + |
| 30 | + - the ``price`` field value equals ``1.99`` *and* |
31 | 31 | - either the ``qty`` field value is less than ``20`` **or** the
|
32 | 32 | ``sale`` field value is ``true``.
|
33 |
| - |
| 33 | + |
34 | 34 | Consider the following example which uses the :operator:`$or` operator to select
|
35 | 35 | fields from embedded documents:
|
36 | 36 |
|
37 | 37 | .. code-block:: javascript
|
38 |
| - |
| 38 | + |
39 | 39 | db.inventory.update( { $or: [ { price:10.99 }, { "carrier.state": "NY"} ] }, { $set: { sale: true } } )
|
40 |
| - |
| 40 | + |
41 | 41 | This :method:`update() <db.collection.update()>` operation will set
|
42 | 42 | the value of the ``sale`` field in the documents in the
|
43 | 43 | ``inventory`` collection where:
|
44 |
| - |
45 |
| - - the ``price`` field value equals ``10.99`` **or** |
| 44 | + |
| 45 | + - the ``price`` field value equals ``10.99`` **or** |
46 | 46 | - the ``carrier`` embedded document contains a field ``state`` whose
|
47 | 47 | value equals ``NY``.
|
48 |
| - |
| 48 | + |
49 | 49 | When using :operator:`$or` with ``<expressions>`` that are equality
|
50 | 50 | checks for the value of the same field, choose the :operator:`$in`
|
51 | 51 | operator over the :operator:`$or` operator.
|
52 |
| - |
| 52 | + |
53 | 53 | Consider the query to select all documents in the ``inventory``
|
54 | 54 | collection where:
|
55 |
| - |
| 55 | + |
56 | 56 | - either ``price`` field value equals ``1.99`` *or* the ``sale``
|
57 | 57 | field value equals ``true``, **and**
|
58 | 58 | - either ``qty`` field value equals ``20`` *or* ``qty`` field value
|
59 | 59 | equals ``50``,
|
60 |
| - |
| 60 | + |
61 | 61 | The most effective query would be:
|
62 |
| - |
| 62 | + |
63 | 63 | .. code-block:: javascript
|
64 |
| - |
| 64 | + |
65 | 65 | db.inventory.find ( { $or: [ { price: 1.99 }, { sale: true } ], qty: { $in: [20, 50] } } )
|
66 | 66 |
|
67 | 67 | Consider the following behaviors when using the :operator:`$or`
|
68 | 68 | operator:
|
69 |
| - |
| 69 | + |
70 | 70 | - When using indexes with :operator:`$or` queries, remember that
|
71 | 71 | each clause of an :operator:`$or` query will execute in parallel.
|
72 | 72 | These clauses can each use their own index. Consider the following
|
|
93 | 93 | This modified query will not use the index on ``price`` nor the
|
94 | 94 | index on ``sale``.
|
95 | 95 |
|
| 96 | + - You cannot use the :operator:`$or` with ``2d`` :doc:`geospatial |
| 97 | + queries </core/geospatial-indexes>`. |
| 98 | + |
96 | 99 | .. seealso:: :method:`find() <db.collection.find()>`,
|
97 | 100 | :method:`update() <db.collection.update()>`, :operator:`$set`,
|
98 | 101 | :operator:`$and`, :method:`sort() <cursor.sort()>`.
|
0 commit comments