forked from mongodb/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathand.txt
More file actions
67 lines (44 loc) · 2.29 KB
/
Copy pathand.txt
File metadata and controls
67 lines (44 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
====
$and
====
.. default-domain:: mongodb
.. operator:: $and
.. versionadded:: 2.0
*Syntax*: ``{ $and: [ { <expression1> }, { <expression2> } , ... , { <expressionN> } ] }``
:operator:`$and` performs a logical ``AND`` operation on an array
of *two or more* expressions (e.g. ``<expression1>``,
``<expression2>``, etc.) and selects the documents that satisfy
*all* the expressions in the array. The :operator:`$and` operator
uses *short-circuit evaluation*. If the first expression
(e.g. ``<expression1>``) evaluates to ``false``, MongoDB will not
evaluate the remaining expressions.
Consider the following example:
.. code-block:: javascript
db.inventory.find({ $and: [ { price: 1.99 }, { qty: { $lt: 20 } }, { sale: true } ] } )
This query will select all documents in the ``inventory``
collection where:
- ``price`` field value equals ``1.99`` **and**
- ``qty`` field value is less than ``20`` **and**
- ``sale`` field value is equal to ``true``.
MongoDB provides an implicit ``AND`` operation when specifying a
comma separated list of expressions. For example, you may write the
above query as:
.. code-block:: javascript
db.inventory.find( { price: 1.99, qty: { $lt: 20 } , sale: true } )
If, however, a query requires an ``AND`` operation on the same field
such as ``{ price: { $ne: 1.99 } } AND { price: { $exists: true }
}``, then either use the :operator:`$and` operator for the two
separate expressions or combine the operator expressions for the
field ``{ price: { $ne: 1.99, $exists: true } }``.
Consider the following examples:
.. code-block:: javascript
db.inventory.update( { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] }, { $set: { qty: 15 } } )
db.inventory.update( { price: { $ne: 1.99, $exists: true } } , { $set: { qty: 15 } } )
Both :method:`update() <db.collection.update()>` operations will set
the value of the ``qty`` field in documents where:
- the ``price`` field value does not equal ``1.99`` **and**
- the ``price`` field exists.
.. seealso::
:method:`find() <db.collection.find()>`, :method:`update()
<db.collection.update()>`, :operator:`$ne`, :operator:`$exists`,
:operator:`$set`.