$all¶
-
$all
¶ Syntax:
{ field: { $all: [ <value> , <value1> ... ] }
$all
selects the documents where thefield
holds an array and contains all elements (e.g.<value>
,<value1>
, etc.) in the array.Consider the following example:
This query selects all documents in the
inventory
collection where thetags
field contains an array with the elements,appliances
,school
, andbook
.Therefore, the above query will match documents in the
inventory
collection that have atags
field that hold either of the following arrays:The
$all
operator exists to describe and specify arrays in MongoDB queries. However, you may use the$all
operator to select against a non-arrayfield
, as in the following example:However, use the following form to express the same query:
Both queries will select all documents in the
inventory
collection where the value of theqty
field equals50
.Note
In most cases, MongoDB does not treat arrays as sets. This operator provides a notable exception to this approach.
In the current release queries that use the
$all
operator must scan all the documents that match the first element in the query array. As a result, even with an index to support the query, the operation may be long running, particularly when the first element in the array is not very selective.