Skip to content

Commit 0f9d5c4

Browse files
jason-price-mongodbjason-price-mongodb
andauthored
DOCSP-24657 index of array example issue (#1705) (#1709)
* DOCSP-indexOfArray-example-issue * DOCSP-indexOfArray-example-issue * DOCSP-24657-indexOfArray-example-issue Co-authored-by: jason-price-mongodb <[email protected]> Co-authored-by: jason-price-mongodb <[email protected]>
1 parent 4bf39fe commit 0f9d5c4

File tree

1 file changed

+43
-53
lines changed

1 file changed

+43
-53
lines changed

source/reference/operator/aggregation/indexOfArray.txt

Lines changed: 43 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,16 @@ Definition
2828

2929
{ $indexOfArray: [ <array expression>, <search expression>, <start>, <end> ] }
3030

31-
3231
.. list-table::
3332
:header-rows: 1
3433
:widths: 20 20 80
3534

3635
* - Field
37-
3836
- Type
39-
4037
- Description
4138

42-
* - ``<array>``
43-
39+
* - ``<array>``
4440
- string
45-
4641
- Can be any valid :ref:`expression <aggregation-expressions>` as long
4742
as it resolves to an array. For more information on expressions, see
4843
:ref:`aggregation-expressions`.
@@ -54,36 +49,24 @@ Definition
5449
If the array expression does not resolve to an array or ``null`` nor
5550
refers to a missing field, :expression:`$indexOfArray` returns an
5651
error.
57-
58-
59-
60-
* - ``<search value>``
61-
52+
53+
* - ``<search value>``
6254
- string
63-
6455
- Can be any valid :ref:`expression <aggregation-expressions>`. For
6556
more information on expressions, see :ref:`aggregation-expressions`.
66-
67-
6857

6958
* - ``<start>``
70-
7159
- integer
72-
7360
- Optional. An integer, or a number that can be represented as integers (such as
7461
2.0), that specifies the starting index position for the search. Can
7562
be any valid :ref:`expression <aggregation-expressions>` that
7663
resolves to a non-negative integral number.
7764

7865
If unspecified, the starting index position for the search is the
7966
beginning of the string.
80-
81-
82-
83-
* - ``<end>``
84-
67+
68+
* - ``<end>``
8569
- integer
86-
8770
- Optional. An integer, or a number that can be represented as integers (such as
8871
2.0), that specifies the ending index position for the search. Can
8972
be any valid :ref:`expression <aggregation-expressions>` that
@@ -94,10 +77,6 @@ Definition
9477

9578
If unspecified, the ending index position for the search is the
9679
end of the string.
97-
98-
99-
100-
10180

10281
Behavior
10382
--------
@@ -169,44 +148,55 @@ position.
169148
* - ``{ $indexOfArray: [ "foo", "foo" ] }``
170149
- Error
171150

172-
Examples
173-
--------
151+
Example
152+
-------
174153

175-
Consider an ``inventory`` collection with the following documents:
154+
The example uses this ``inventory`` collection:
176155

177156
.. code-block:: javascript
178157

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+
] )
184167

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:
187170

188171
.. code-block:: javascript
189172

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+
} ] )
200178

201-
The operation returns the following results:
179+
The example returns:
202180

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.
204186

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:
210188

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+
]
211201

212202
.. seealso:: :expression:`$indexOfBytes`, :expression:`$indexOfCP`, and :expression:`$in`

0 commit comments

Comments
 (0)