|
| 1 | +=========================== |
| 2 | +$indexOfArray (aggregation) |
| 3 | +=========================== |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 1 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +Definition |
| 14 | +---------- |
| 15 | + |
| 16 | +.. expression:: $indexOfArray |
| 17 | + |
| 18 | + .. versionadded:: 3.4 |
| 19 | + |
| 20 | + Searches an array for an occurence of a specified value and returns |
| 21 | + the array index (zero-based) of the first occurence. If the |
| 22 | + value is not found, returns ``-1``. |
| 23 | + |
| 24 | + :expression:`$indexOfArray` has the following :ref:`operator |
| 25 | + expression syntax <agg-quick-ref-operator-expressions>`: |
| 26 | + |
| 27 | + .. code-block:: javascript |
| 28 | + |
| 29 | + { $indexOfArray: [ <array expression>, <search expression>, <start>, <end> ] } |
| 30 | + |
| 31 | + .. include:: /includes/apiargs/agg-indexOfArray-fields.rst |
| 32 | + |
| 33 | +Behavior |
| 34 | +-------- |
| 35 | + |
| 36 | +If the ``<search expression>`` is found multiple times within the |
| 37 | +``<array expression>``, then :expression:`$indexOfArray` returns the |
| 38 | +index of the first ``<search expression>`` from the starting index |
| 39 | +position. |
| 40 | + |
| 41 | +:expression:`$indexOfArray` returns ``null``: |
| 42 | + |
| 43 | +- If ``<array expression>`` is null, or |
| 44 | + |
| 45 | +- If ``<array expression>`` refers to a non-existing field in the input |
| 46 | + document. |
| 47 | + |
| 48 | +:expression:`$indexOfArray` returns an error: |
| 49 | + |
| 50 | +- If ``<array expression>`` is not an array and not null, or |
| 51 | + |
| 52 | +- If ``<start>`` or ``<end>`` is a negative integer (or a value that |
| 53 | + can be represented as a negative integer, like -5.0). |
| 54 | + |
| 55 | +:expression:`$indexOfArray` returns ``-1``: |
| 56 | + |
| 57 | +- If the <search expression> is not found in the array, or |
| 58 | + |
| 59 | +- If ``<start>`` is a number greater than ``<end>``, or |
| 60 | + |
| 61 | +- If ``<start>`` is a number greater than the length of the array. |
| 62 | + |
| 63 | +.. list-table:: |
| 64 | + :header-rows: 1 |
| 65 | + :widths: 95 5 |
| 66 | + |
| 67 | + * - Example |
| 68 | + - Results |
| 69 | + |
| 70 | + * - ``{ $indexOfArray: [ [ "a", "abc" ], "a" ] }`` |
| 71 | + - ``0`` |
| 72 | + |
| 73 | + * - ``{ $indexOfArray: [ [ "a", "abc", "de", ["de"] ], ["de"] ] }`` |
| 74 | + - ``3`` |
| 75 | + |
| 76 | + * - ``{ $indexOfArray: [ [ 1, 2 ], 5 ] }`` |
| 77 | + - ``-1`` |
| 78 | + |
| 79 | + * - ``{ $indexOfArray: [ [ 1, 2, 3 ], [1, 2] ] }`` |
| 80 | + - ``-1`` |
| 81 | + |
| 82 | + * - ``{ $indexOfArray: [ [ 10, 9, 9, 8, 9 ], 9, 3 ] }`` |
| 83 | + - ``4`` |
| 84 | + |
| 85 | + * - ``{ $indexOfArray: [ [ "a", "abc", "b" ], "b", 0, 1 ] }`` |
| 86 | + - ``-1`` |
| 87 | + |
| 88 | + * - ``{ $indexOfArray: [ [ "a", "abc", "b" ], "b", 1, 0 ] }`` |
| 89 | + - ``-1`` |
| 90 | + |
| 91 | + * - ``{ $indexOfArray: [ [ "a", "abc", "b" ], "b", 20 ] }`` |
| 92 | + - ``-1`` |
| 93 | + |
| 94 | + * - ``{ $indexOfArray: [ [ null, null, null ], null ] }`` |
| 95 | + - ``0`` |
| 96 | + |
| 97 | + * - ``{ $indexOfArray: [ null, "foo" ] }`` |
| 98 | + - ``null`` |
| 99 | + |
| 100 | + * - ``{ $indexOfArray: [ "foo", "foo" ] }`` |
| 101 | + - Error |
| 102 | + |
| 103 | +Examples |
| 104 | +-------- |
| 105 | + |
| 106 | +Consider an ``inventory`` collection with the following documents: |
| 107 | + |
| 108 | +.. code-block:: javascript |
| 109 | + |
| 110 | + { "_id" : 1, "items" : ["one", "two", "three"] } |
| 111 | + { "_id" : 2, "items" : [1, 2, 3] } |
| 112 | + { "_id" : 3, "items" : [null, null, 2] } |
| 113 | + { "_id" : 4, "items" : null } |
| 114 | + { "_id" : 5, "amount" : 3 } |
| 115 | + |
| 116 | +The following operation uses the :expression:`$indexOfArray` operator to |
| 117 | +return the array index at which the string ``foo`` is located in each ``items`` array: |
| 118 | + |
| 119 | +.. code-block:: javascript |
| 120 | + |
| 121 | + db.inventory.aggregate( |
| 122 | + [ |
| 123 | + { |
| 124 | + $project: |
| 125 | + { |
| 126 | + index: { $indexOfArray: [ "$items", 2 ] }, |
| 127 | + } |
| 128 | + } |
| 129 | + ] |
| 130 | + ) |
| 131 | + |
| 132 | +The operation returns the following results: |
| 133 | + |
| 134 | +.. code-block:: javascript |
| 135 | + |
| 136 | + { "_id" : 1, "index" : "-1" } |
| 137 | + { "_id" : 2, "index" : "1" } |
| 138 | + { "_id" : 3, "index" : "2" } |
| 139 | + { "_id" : 4, "index" : null } |
| 140 | + { "_id" : 5, "index" : null } |
| 141 | + |
| 142 | + |
| 143 | +.. seealso:: :expression:`$indexOfBytes`, :expression:`$indexOfCP`, and :expression:`$in` |
0 commit comments