/
in.txt
107 lines (72 loc) · 3.19 KB
/
in.txt
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
===
$in
===
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
.. query:: $in
The :query:`$in` operator selects the documents where the value
of a field equals any value in the specified array. To specify an
:query:`$in` expression, use the following prototype:
.. include:: /includes/fact-comparison-order.rst
.. code-block:: javascript
{ field: { $in: [<value1>, <value2>, ... <valueN> ] } }
If the ``field`` holds an array, then the :query:`$in` operator
selects the documents whose ``field`` holds an array that contains
at least one element that matches a value in the specified array
(for example, ``<value1>``, ``<value2>``, and so on).
.. note::
This document describes the :query:`$in` query operator.
For the ``$in`` aggregation operator, see
:doc:`/reference/operator/aggregation/in`.
.. versionchanged:: 2.6
MongoDB 2.6 removes the combinatorial limit for the :query:`$in`
operator that exists for :v2.4:`earlier versions
</reference/operator/query/in>` of the operator.
Examples
--------
Use the ``$in`` Operator to Match Values
----------------------------------------
Consider the following example:
.. code-block:: javascript
db.inventory.find( { qty: { $in: [ 5, 15 ] } } )
This query selects all documents in the ``inventory``
collection where the ``qty`` field value is either ``5`` or
``15``. Although you can express this query using the
:query:`$or` operator, choose the :query:`$in` operator rather
than the :query:`$or` operator when performing equality checks on
the same field.
Use the ``$in`` Operator to Match Values in an Array
----------------------------------------------------
The collection ``inventory`` contains documents that include the field
``tags``, as in the following:
.. code-block:: javascript
{ _id: 1, item: "abc", qty: 10, tags: [ "school", "clothing" ], sale: false }
Then, the following :method:`~db.collection.update()` operation will
set the ``sale`` field value to ``true`` where the ``tags`` field holds
an array with at least one element matching either ``"appliances"`` or
``"school"``.
.. code-block:: javascript
db.inventory.update(
{ tags: { $in: ["appliances", "school"] } },
{ $set: { sale:true } }
)
.. include:: /includes/extracts/additional-examples-arrays.rst
.. _query-in-regex:
Use the ``$in`` Operator with a Regular Expression
--------------------------------------------------
The :query:`$in` operator can specify matching values using regular
expressions of the form ``/pattern/``. You *cannot* use :query:`$regex`
operator expressions inside an :query:`$in`.
Consider the following example:
.. code-block:: javascript
db.inventory.find( { tags: { $in: [ /^be/, /^st/ ] } } )
This query selects all documents in the ``inventory`` collection where
the ``tags`` field holds either a string that starts with ``be`` or
``st`` or an array with at least one element that starts with ``be`` or
``st``.
.. seealso::
:method:`~db.collection.find()`, :method:`~db.collection.update()`, :query:`$or`, :update:`$set`, :query:`$elemMatch`.