14
14
15
15
*Syntax*: ``{ $and: [ { <expression1> }, { <expression2> } , ... , { <expressionN> } ] }``
16
16
17
- :query:`$and` performs a logical ``AND`` operation on an array
18
- of *one or more* expressions (e.g. ``<expression1>``,
19
- ``<expression2>``, etc.) and selects the documents that satisfy
20
- *all* the expressions in the array. The :query:`$and` operator
21
- uses *short-circuit evaluation*. If the first expression
22
- (e.g. ``<expression1>``) evaluates to ``false``, MongoDB will not
23
- evaluate the remaining expressions.
17
+ :query:`$and` performs a logical ``AND`` operation on an array of
18
+ *one or more* expressions (``<expression1>``, ``<expression2>``, and
19
+ so on) and selects the documents that satisfy *all* the expressions.
24
20
25
21
.. note::
26
22
27
23
MongoDB provides an implicit ``AND`` operation when specifying a
28
24
comma separated list of expressions.
29
25
26
+ Behavior
27
+ --------
28
+
29
+ .. |and-or| replace:: ``$and``
30
+ .. |true-false| replace:: ``false``
31
+
32
+ .. include:: /includes/and-or-behavior.rst
33
+
34
+ .. code-block:: javascript
35
+
36
+ db.example.find( {
37
+ $and: [
38
+ { x: { $ne: 0 } },
39
+ { $expr: { $eq: [ { $divide: [ 1, "$x" ] }, 3 ] } }
40
+ ]
41
+ } )
42
+
30
43
Examples
31
44
--------
32
45
33
46
``AND`` Queries With Multiple Expressions Specifying the Same Field
34
47
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
35
48
36
- Consider the following example :
49
+ Consider this query :
37
50
38
51
.. code-block:: javascript
39
52
40
53
db.inventory.find( { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } )
41
54
42
- This query will select all documents in the ``inventory``
43
- collection where:
55
+ The query selects all documents in the ``inventory`` collection where:
44
56
45
57
- the ``price`` field value is not equal to ``1.99`` **and**
46
58
- the ``price`` field exists.
47
59
48
- This query can be also be constructed with an implicit ``AND``
49
- operation by combining the operator expressions for the ``price``
50
- field. For example, this query can be written as:
60
+ The query can be rewritten with an implicit ``AND`` operation that
61
+ combines the operator expressions for the ``price`` field:
51
62
52
63
.. code-block:: javascript
53
64
@@ -56,7 +67,7 @@ field. For example, this query can be written as:
56
67
``AND`` Queries With Multiple Expressions Specifying the Same Operator
57
68
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
58
69
59
- Consider the following example :
70
+ Consider this query :
60
71
61
72
.. code-block:: javascript
62
73
@@ -67,14 +78,14 @@ Consider the following example:
67
78
]
68
79
} )
69
80
70
- This query will select all documents where:
81
+ The query selects all documents where:
71
82
72
83
- the ``qty`` field value is less than ``10`` or greater than ``50``, **and**
73
84
- the ``sale`` field value is equal to ``true`` **or** the ``price``
74
85
field value is less than ``5``.
75
86
76
- This query cannot be constructed using an implicit ``AND`` operation,
77
- because it uses the :query:`$or` operator more than once.
87
+ The query cannot use an implicit ``AND`` operation because it uses the
88
+ :query:`$or` operator more than once.
78
89
79
90
.. seealso::
80
91
0 commit comments