$size
$size
- The
$size
operator matches any array with the number ofelements specified by the argument. For example:
- db.collection.find( { field: { $size: 2 } } );
returns all documents in collection
where field
is an arraywith 2 elements. For instance, the above expression willreturn { field: [ red, green ] }
and { field: [ apple,lime ] }
but not { field: fruit }
or { field: [orange, lemon, grapefruit ] }
. To match fields with only oneelement within an array use $size
with a value of 1, asfollows:
- db.collection.find( { field: { $size: 1 } } );
$size
does not accept ranges of values. To selectdocuments based on fields with different numbers of elements,create a counter field that you increment when you add elements toa field.
Queries cannot use indexes for the $size
portion of aquery, although the other portions of a query can use indexes ifapplicable.
Additional Examples
For additional examples in querying arrays, see:
For additional examples in querying, see:
See also