1616 of a field equals any value in the specified array. To specify an
1717 :query:`$in` expression, use the following prototype:
1818
19- .. include:: /includes/fact-comparison-order.rst
20-
2119 .. code-block:: javascript
2220
2321 { field: { $in: [<value1>, <value2>, ... <valueN> ] } }
2422
23+ .. include:: /includes/fact-comparison-order.rst
24+
2525 If the ``field`` holds an array, then the :query:`$in` operator
2626 selects the documents whose ``field`` holds an array that contains
2727 at least one element that matches a value in the specified array
3636Examples
3737--------
3838
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+
3950Use the ``$in`` Operator to Match Values
40- ----------------------------------------
51+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4152
4253Consider the following example:
4354
4455.. code-block:: javascript
4556
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.
4761
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.
5470
5571Use the ``$in`` Operator to Match Values in an Array
56- ----------------------------------------------------
72+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5773
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"``.
6077
6178.. code-block:: javascript
6279
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+ )
6484
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:
6986
7087.. code-block:: javascript
7188
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+ }
76111
77112.. include:: /includes/extracts/additional-examples-arrays.rst
78113
@@ -99,7 +134,8 @@ the ``tags`` field holds either a string that starts with ``be`` or
99134.. seealso::
100135
101136 - :method:`~db.collection.find()`
102- - :method:`~db.collection.update ()`
137+ - :method:`~db.collection.updateMany ()`
103138 - :query:`$or`
104139 - :update:`$set`
105140 - :query:`$elemMatch`
141+
0 commit comments