- Reference >
- Operators >
- Query and Projection Operators >
- Query Operator Array >
- $size
$size¶
-
$size¶ The
$sizeoperator matches any array with the number of elements specified by the argument. For example:returns all documents in
collectionwherefieldis an array with 2 elements. For instance, the above expression will return{ field: [ red, green ] }and{ field: [ apple, lime ] }but not{ field: fruit }or{ field: [ orange, lemon, grapefruit ] }. To match fields with only one element within an array use$sizewith a value of 1, as follows:$sizedoes not accept ranges of values. To select documents based on fields with different numbers of elements, create a counter field that you increment when you add elements to a field.Queries cannot use indexes for the
$sizeportion of a query, although the other portions of a query can use indexes if applicable.