@@ -28,21 +28,16 @@ Definition
28
28
29
29
{ $indexOfArray: [ <array expression>, <search expression>, <start>, <end> ] }
30
30
31
-
32
31
.. list-table::
33
32
:header-rows: 1
34
33
:widths: 20 20 80
35
34
36
35
* - Field
37
-
38
36
- Type
39
-
40
37
- Description
41
38
42
- * - ``<array>``
43
-
39
+ * - ``<array>``
44
40
- string
45
-
46
41
- Can be any valid :ref:`expression <aggregation-expressions>` as long
47
42
as it resolves to an array. For more information on expressions, see
48
43
:ref:`aggregation-expressions`.
@@ -54,36 +49,24 @@ Definition
54
49
If the array expression does not resolve to an array or ``null`` nor
55
50
refers to a missing field, :expression:`$indexOfArray` returns an
56
51
error.
57
-
58
-
59
-
60
- * - ``<search value>``
61
-
52
+
53
+ * - ``<search value>``
62
54
- string
63
-
64
55
- Can be any valid :ref:`expression <aggregation-expressions>`. For
65
56
more information on expressions, see :ref:`aggregation-expressions`.
66
-
67
-
68
57
69
58
* - ``<start>``
70
-
71
59
- integer
72
-
73
60
- Optional. An integer, or a number that can be represented as integers (such as
74
61
2.0), that specifies the starting index position for the search. Can
75
62
be any valid :ref:`expression <aggregation-expressions>` that
76
63
resolves to a non-negative integral number.
77
64
78
65
If unspecified, the starting index position for the search is the
79
66
beginning of the string.
80
-
81
-
82
-
83
- * - ``<end>``
84
-
67
+
68
+ * - ``<end>``
85
69
- integer
86
-
87
70
- Optional. An integer, or a number that can be represented as integers (such as
88
71
2.0), that specifies the ending index position for the search. Can
89
72
be any valid :ref:`expression <aggregation-expressions>` that
@@ -94,10 +77,6 @@ Definition
94
77
95
78
If unspecified, the ending index position for the search is the
96
79
end of the string.
97
-
98
-
99
-
100
-
101
80
102
81
Behavior
103
82
--------
@@ -169,44 +148,55 @@ position.
169
148
* - ``{ $indexOfArray: [ "foo", "foo" ] }``
170
149
- Error
171
150
172
- Examples
173
- --------
151
+ Example
152
+ -------
174
153
175
- Consider an ``inventory`` collection with the following documents :
154
+ The example uses this ``inventory`` collection:
176
155
177
156
.. code-block:: javascript
178
157
179
- { "_id" : 1, "items" : ["one", "two", "three"] }
180
- { "_id" : 2, "items" : [1, 2, 3] }
181
- { "_id" : 3, "items" : [null, null, 2] }
182
- { "_id" : 4, "items" : null }
183
- { "_id" : 5, "amount" : 3 }
158
+ db.inventory.insertMany( [
159
+ { _id: 0, items: [ "one", "two", "three" ] },
160
+ { _id: 1, items: [ 1, 2, 3 ] },
161
+ { _id: 2, items: [ 1, 2, 3, 2 ] },
162
+ { _id: 3, items: [ null, null, 2 ] },
163
+ { _id: 4, items: [ 2, null, null, 2 ] },
164
+ { _id: 5, items: null },
165
+ { _id: 6, amount: 3 }
166
+ ] )
184
167
185
- The following operation uses the :expression:`$indexOfArray` operator to
186
- return the array index at which the string ``foo`` is located in each ``items`` array:
168
+ The following example uses :expression:`$indexOfArray` to find ``2`` in
169
+ the ``items`` array:
187
170
188
171
.. code-block:: javascript
189
172
190
- db.inventory.aggregate(
191
- [
192
- {
193
- $project:
194
- {
195
- index: { $indexOfArray: [ "$items", 2 ] },
196
- }
197
- }
198
- ]
199
- )
173
+ db.inventory.aggregate( [ {
174
+ $project: {
175
+ index: { $indexOfArray: [ "$items", 2 ] }
176
+ }
177
+ } ] )
200
178
201
- The operation returns the following results :
179
+ The example returns:
202
180
203
- .. code-block:: javascript
181
+ - The first array index for the value ``2`` in each ``items`` array, if
182
+ found. Array indexes start at ``0``.
183
+ - ``-1`` for the index if ``2`` is not in the ``items`` array.
184
+ - ``null`` for the index if ``items`` is not an array or ``items`` does
185
+ not exist.
204
186
205
- { "_id" : 1, "index" : "-1" }
206
- { "_id" : 2, "index" : "1" }
207
- { "_id" : 3, "index" : "2" }
208
- { "_id" : 4, "index" : null }
209
- { "_id" : 5, "index" : null }
187
+ Example output:
210
188
189
+ .. code-block:: javascript
190
+ :copyable: false
191
+
192
+ [
193
+ { _id: 0, index: -1 },
194
+ { _id: 1, index: 1 },
195
+ { _id: 2, index: 1 },
196
+ { _id: 3, index: 2 },
197
+ { _id: 4, index: 0 },
198
+ { _id: 5, index: null },
199
+ { _id: 6, index: null }
200
+ ]
211
201
212
202
.. seealso:: :expression:`$indexOfBytes`, :expression:`$indexOfCP`, and :expression:`$in`
0 commit comments