16
16
of a field equals any value in the specified array. To specify an
17
17
:query:`$in` expression, use the following prototype:
18
18
19
- .. include:: /includes/fact-comparison-order.rst
20
-
21
19
.. code-block:: javascript
22
20
23
21
{ field: { $in: [<value1>, <value2>, ... <valueN> ] } }
24
22
23
+ .. include:: /includes/fact-comparison-order.rst
24
+
25
25
If the ``field`` holds an array, then the :query:`$in` operator
26
26
selects the documents whose ``field`` holds an array that contains
27
27
at least one element that matches a value in the specified array
36
36
Examples
37
37
--------
38
38
39
+ Create the ``inventory`` collection:
40
+
41
+ .. code-block:: javascript
42
+
43
+ db.inventory.insertMany( [
44
+ { "item": "Pens", "quantity": 350, "tags": [ "school", "office" ] },
45
+ { "item": "Erasers", "quantity": 15, "tags": [ "school", "home" ] },
46
+ { "item": "Maps", "tags": [ "office", "storage" ] },
47
+ { "item": "Books", "quantity": 5, "tags": [ "school", "storage", "home" ] }
48
+ ] )
49
+
39
50
Use the ``$in`` Operator to Match Values
40
- ----------------------------------------
51
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
41
52
42
53
Consider the following example:
43
54
44
55
.. code-block:: javascript
45
56
46
- db.inventory.find( { qty: { $in: [ 5, 15 ] } } )
57
+ db.inventory.find( { quantity: { $in: [ 5, 15 ] } }, { _id: 0 } )
58
+
59
+ This query selects all documents in the ``inventory`` collection where
60
+ the value of the ``quantity`` field is either 5 or 15.
47
61
48
- This query selects all documents in the ``inventory``
49
- collection where the ``qty`` field value is either ``5`` or
50
- ``15``. Although you can express this query using the
51
- :query:`$or` operator, choose the :query:`$in` operator rather
52
- than the :query:`$or` operator when performing equality checks on
53
- the same field.
62
+ .. code-block:: javascript
63
+
64
+ { item: 'Erasers', quantity: 15, tags: [ 'school', 'home' ] },
65
+ { item: 'Books', quantity: 5, tags: [ 'school', 'storage', 'home' ] }
66
+
67
+ Although you can write this query using the :query:`$or` operator,
68
+ use the :query:`$in` operator rather than the :query:`$or` operator
69
+ when performing equality checks on the same field.
54
70
55
71
Use the ``$in`` Operator to Match Values in an Array
56
- ----------------------------------------------------
72
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
57
73
58
- The collection ``inventory`` contains documents that include the field
59
- ``tags``, as in the following:
74
+ The following :method:`~db.collection.updateMany()` operation sets the
75
+ ``exclude`` field to ``false`` when the ``tags`` array has at least one
76
+ element that matches either ``"home"`` or ``"school"``.
60
77
61
78
.. code-block:: javascript
62
79
63
- { _id: 1, item: "abc", qty: 10, tags: [ "school", "clothing" ], sale: false }
80
+ db.inventory.updateMany(
81
+ { tags: { $in: [ "home", "school" ] } },
82
+ { $set: { exclude: false } }
83
+ )
64
84
65
- Then, the following :method:`~db.collection.update()` operation will
66
- set the ``sale`` field value to ``true`` where the ``tags`` field holds
67
- an array with at least one element matching either ``"appliances"`` or
68
- ``"school"``.
85
+ Example output:
69
86
70
87
.. code-block:: javascript
71
88
72
- db.inventory.update(
73
- { tags: { $in: ["appliances", "school"] } },
74
- { $set: { sale:true } }
75
- )
89
+ {
90
+ item: 'Pens',
91
+ quantity: 350,
92
+ tags: [ 'school', 'office' ],
93
+ exclude: false
94
+ },
95
+ {
96
+ item: 'Erasers',
97
+ quantity: 15,
98
+ tags: [ 'school', 'home' ],
99
+ exclude: false
100
+ },
101
+ {
102
+ item: 'Maps',
103
+ tags: [ 'office', 'storage' ]
104
+ },
105
+ {
106
+ item: 'Books',
107
+ quantity: 5,
108
+ tags: [ 'school', 'storage', 'home' ],
109
+ exclude: false
110
+ }
76
111
77
112
.. include:: /includes/extracts/additional-examples-arrays.rst
78
113
@@ -99,7 +134,8 @@ the ``tags`` field holds either a string that starts with ``be`` or
99
134
.. seealso::
100
135
101
136
- :method:`~db.collection.find()`
102
- - :method:`~db.collection.update ()`
137
+ - :method:`~db.collection.updateMany ()`
103
138
- :query:`$or`
104
139
- :update:`$set`
105
140
- :query:`$elemMatch`
141
+
0 commit comments