|
| 1 | +======================== |
| 2 | +$firstN (array operator) |
| 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:: $firstN |
| 17 | + |
| 18 | + .. versionadded:: 5.2 |
| 19 | + |
| 20 | + Returns a specified number of elements from the beginning of an |
| 21 | + array. |
| 22 | + |
| 23 | +.. seealso:: |
| 24 | + |
| 25 | + - :expression:`$lastN` |
| 26 | + |
| 27 | + - :expression:`$sortArray` |
| 28 | + |
| 29 | +Syntax |
| 30 | +------ |
| 31 | + |
| 32 | +:expression:`$firstN` has the following syntax: |
| 33 | + |
| 34 | +.. code-block:: javascript |
| 35 | + |
| 36 | + { $firstN: { n: <expression>, input: <expression> } } |
| 37 | + |
| 38 | +.. list-table:: |
| 39 | + :header-rows: 1 |
| 40 | + :class: border-table |
| 41 | + |
| 42 | + * - Field |
| 43 | + - Description |
| 44 | + |
| 45 | + * - ``n`` |
| 46 | + - An :ref:`expression <aggregation-expressions>` that resolves to a |
| 47 | + positive integer. The integer specifies the number of array elements |
| 48 | + that :expression:`$firstN` returns. |
| 49 | + |
| 50 | + * - ``input`` |
| 51 | + - An :ref:`expression <aggregation-expressions>` that resolves to the |
| 52 | + array from which to return ``n`` elements. |
| 53 | + |
| 54 | +Behavior |
| 55 | +-------- |
| 56 | + |
| 57 | +- :expression:`$firstN` returns elements in the same order they appear in |
| 58 | + the input array. |
| 59 | + |
| 60 | +- :expression:`$firstN` does not filter out ``null`` values in the input |
| 61 | + array. |
| 62 | + |
| 63 | +- You cannot specify a value of ``n`` less than ``1``. |
| 64 | + |
| 65 | +- If the specified ``n`` is greater than or equal to the number of elements |
| 66 | + in the ``input`` array, :expression:`$firstN` returns the ``input`` array. |
| 67 | + |
| 68 | +- If ``input`` resolves to a non-array value, the aggregation operation |
| 69 | + errors. |
| 70 | + |
| 71 | +Example |
| 72 | +------- |
| 73 | + |
| 74 | +The collection ``games`` has the following documents: |
| 75 | + |
| 76 | +.. code-block:: javascript |
| 77 | + :copyable: true |
| 78 | + |
| 79 | + db.games.insertMany([ |
| 80 | + { "playerId" : 1, "score" : [ 1, 2, 3 ] }, |
| 81 | + { "playerId" : 2, "score" : [ 12, 90, 7, 89, 8 ] }, |
| 82 | + { "playerId" : 3, "score" : [ null ] }, |
| 83 | + { "playerId" : 4, "score" : [ ] }, |
| 84 | + { "playerId" : 5, "score" : [ 1293, null, 3489, 9 ]}, |
| 85 | + { "playerId" : 6, "score" : [ "12.1", 2, NumberLong("2090845886852"), 23 ]} |
| 86 | + ]) |
| 87 | + |
| 88 | +The following example uses the :expression:`$firstN` operator to retrieve the |
| 89 | +first three scores for each player. The scores are returned in the new field |
| 90 | +``firstScores`` created by :pipeline:`$addFields`. |
| 91 | + |
| 92 | +.. code-block:: javascript |
| 93 | + :copyable: true |
| 94 | + |
| 95 | + db.games.aggregate([ |
| 96 | + { $addFields: { firstScores: { $firstN: { n: 3, input: "$score" } } } } |
| 97 | + ]) |
| 98 | + |
| 99 | +The operation returns the following results: |
| 100 | + |
| 101 | +.. code-block:: javascript |
| 102 | + :copyable: true |
| 103 | + :emphasize-lines: 4, 9, 14, 19, 24, 29 |
| 104 | + |
| 105 | + [{ |
| 106 | + "playerId": 1, |
| 107 | + "score": [ 1, 2, 3 ], |
| 108 | + "firstScores": [ 1, 2, 3 ] |
| 109 | + }, |
| 110 | + { |
| 111 | + "playerId": 2, |
| 112 | + "score": [ 12, 90, 7, 89, 8 ], |
| 113 | + "firstScores": [ 12, 90, 7 ] |
| 114 | + }, |
| 115 | + { |
| 116 | + "playerId": 3, |
| 117 | + "score": [ null ], |
| 118 | + "firstScores": [ null ] |
| 119 | + }, |
| 120 | + { |
| 121 | + "playerId": 4, |
| 122 | + "score": [ ], |
| 123 | + "firstScores": [ ] |
| 124 | + }, |
| 125 | + { |
| 126 | + "playerId": 5, |
| 127 | + "score": [ 1293, null, 3489, 9 ], |
| 128 | + "firstScores": [ 1293, null, 3489 ] |
| 129 | + }, |
| 130 | + { |
| 131 | + "playerId": 6, |
| 132 | + "score": [ "12.1", 2, NumberLong("2090845886852"), 23 ], |
| 133 | + "firstScores": [ "12.1", 2, NumberLong("2090845886852") ] |
| 134 | + }] |
| 135 | + |
0 commit comments