|
| 1 | +.. _maxN: |
| 2 | + |
| 3 | +====================== |
| 4 | +$maxN (array operator) |
| 5 | +====================== |
| 6 | + |
| 7 | +.. default-domain:: mongodb |
| 8 | + |
| 9 | +.. contents:: On this page |
| 10 | + :local: |
| 11 | + :backlinks: none |
| 12 | + :depth: 1 |
| 13 | + :class: singlecol |
| 14 | + |
| 15 | +Definition |
| 16 | +---------- |
| 17 | + |
| 18 | +.. expression:: $maxN |
| 19 | + |
| 20 | + .. versionadded:: 5.2 |
| 21 | + |
| 22 | + Returns the ``n`` largest values in an array. |
| 23 | + |
| 24 | +.. seealso:: |
| 25 | + |
| 26 | + :expression:`$minN` |
| 27 | + |
| 28 | +Syntax |
| 29 | +------ |
| 30 | + |
| 31 | +:expression:`$maxN` has the following syntax: |
| 32 | + |
| 33 | +.. code-block:: javascript |
| 34 | + |
| 35 | + { $maxN: { n: <expression>, input: <expression> } } |
| 36 | + |
| 37 | +.. list-table:: |
| 38 | + :header-rows: 1 |
| 39 | + :class: border-table |
| 40 | + |
| 41 | + * - Field |
| 42 | + - Description |
| 43 | + |
| 44 | + * - ``n`` |
| 45 | + - An :ref:`expression <aggregation-expressions>` that resolves to a |
| 46 | + positive integer. The integer specifies the number of array elements |
| 47 | + that :expression:`$maxN` returns. |
| 48 | + |
| 49 | + * - ``input`` |
| 50 | + - An :ref:`expression <aggregation-expressions>` that resolves to the |
| 51 | + array from which to return the maximal ``n`` elements. |
| 52 | + |
| 53 | +Behavior |
| 54 | +-------- |
| 55 | + |
| 56 | +- You cannot specify a value of ``n`` less than ``1``. |
| 57 | + |
| 58 | +- :expression:`$maxN` filters out ``null`` values found in the ``input`` |
| 59 | + array. |
| 60 | + |
| 61 | +- If the specified ``n`` is greater than or equal to the number of elements |
| 62 | + in the ``input`` array, :expression:`$maxN` returns all elements in the |
| 63 | + ``input`` array. |
| 64 | + |
| 65 | +- If ``input`` resolves to a non-array value, the aggregation |
| 66 | + operation errors. |
| 67 | + |
| 68 | +- If ``input`` contains both numeric and string elements, the string elements |
| 69 | + are sorted before numeric elements according to the |
| 70 | + :ref:`BSON comparison order <bson-types-comparison-order>`. |
| 71 | + |
| 72 | +Example |
| 73 | +------- |
| 74 | + |
| 75 | +Create a ``scores`` collection with the following documents: |
| 76 | + |
| 77 | +.. code-block:: javascript |
| 78 | + :copyable: true |
| 79 | + |
| 80 | + db.scores.insertMany([ |
| 81 | + { "playerId" : 1, "score" : [ 1, 2, 3 ] }, |
| 82 | + { "playerId" : 2, "score" : [ 12, 90, 7, 89, 8 ] }, |
| 83 | + { "playerId" : 3, "score" : [ null ] }, |
| 84 | + { "playerId" : 4, "score" : [ ] } |
| 85 | + { "playerId" : 5, "score" : [ 1293, "2", 3489, 9 ]} |
| 86 | + ]) |
| 87 | + |
| 88 | +The following example uses the :expression:`$maxN` operator to retrieve the two |
| 89 | +highest scores for each player. The highest scores are returned in the new field |
| 90 | +``maxScores`` created by :pipeline:`$addFields`. |
| 91 | + |
| 92 | +.. code-block:: javascript |
| 93 | + :copyable: true |
| 94 | + |
| 95 | + db.scores.aggregate([ |
| 96 | + { $addFields: { maxScores: { $maxN: { n: 2, 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 |
| 104 | + |
| 105 | + [{ |
| 106 | + "playerId": 1, |
| 107 | + "score": [ 1, 2, 3 ], |
| 108 | + "maxScores": [ 3, 2 ] |
| 109 | + }, |
| 110 | + { |
| 111 | + "playerId": 2, |
| 112 | + "score": [ 12, 90, 7, 89, 8 ], |
| 113 | + "maxScores": [ 90, 89 ] |
| 114 | + }, |
| 115 | + { |
| 116 | + "playerId": 3, |
| 117 | + "score": [ null ], |
| 118 | + "maxScores": [ ] |
| 119 | + }, |
| 120 | + { |
| 121 | + "playerId": 4, |
| 122 | + "score": [ ], |
| 123 | + "maxScores": [ ] |
| 124 | + }, |
| 125 | + { |
| 126 | + "playerId": 5, |
| 127 | + "score": [ 1293, "2", 3489, 9 ], |
| 128 | + "maxScores": [ "2", 3489 ] |
| 129 | + }] |
0 commit comments