- Reference >
- Operators >
- Aggregation Pipeline Operators >
- $minN (array operator)
$minN (array operator)¶
On this page
Syntax¶
$minN has the following syntax:
| Field | Description |
|---|---|
n |
An expression that resolves to a
positive integer. The integer specifies the number of array elements
that $minN returns. |
input |
An expression that resolves to the
array from which to return the minimal n elements. |
Behavior¶
- You cannot specify a value of
nless than1. $minNfilters outnullvalues found in theinputarray.- If the specified
nis greater than or equal to the number of elements in theinputarray,$minNreturns all elements in theinputarray. - If
inputresolves to a non-array value, the aggregation operation errors. - If
inputcontains both numeric and string elements, the numeric elements are sorted before string elements according to the BSON comparison order.
Example¶
Create a scores collection with the following documents:
The following example uses the $minN operator to retrieve the two
lowest scores for each player. The lowest scores are returned in the new field
minScores created by $addFields.
The operation returns the following results: