1414
1515 *Syntax*: ``{ $and: [ { <expression1> }, { <expression2> } , ... , { <expressionN> } ] }``
1616
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.
2420
2521 .. note::
2622
2723 MongoDB provides an implicit ``AND`` operation when specifying a
2824 comma separated list of expressions.
2925
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+
3043Examples
3144--------
3245
3346``AND`` Queries With Multiple Expressions Specifying the Same Field
3447~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3548
36- Consider the following example :
49+ Consider this query :
3750
3851.. code-block:: javascript
3952
4053 db.inventory.find( { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } )
4154
42- This query will select all documents in the ``inventory``
43- collection where:
55+ The query selects all documents in the ``inventory`` collection where:
4456
4557- the ``price`` field value is not equal to ``1.99`` **and**
4658- the ``price`` field exists.
4759
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:
5162
5263.. code-block:: javascript
5364
@@ -56,7 +67,7 @@ field. For example, this query can be written as:
5667``AND`` Queries With Multiple Expressions Specifying the Same Operator
5768~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5869
59- Consider the following example :
70+ Consider this query :
6071
6172.. code-block:: javascript
6273
@@ -67,14 +78,14 @@ Consider the following example:
6778 ]
6879 } )
6980
70- This query will select all documents where:
81+ The query selects all documents where:
7182
7283- the ``qty`` field value is less than ``10`` or greater than ``50``, **and**
7384- the ``sale`` field value is equal to ``true`` **or** the ``price``
7485 field value is less than ``5``.
7586
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.
7889
7990.. seealso::
8091
0 commit comments