$all¶
On this page
-
$all
¶ The
$all
operator selects the documents where the value of a field is an array that contains all the specified elements. To specify an$all
expression, use the following prototype:
Examples¶
The following examples use the inventory
collection that contains
the documents:
Use $all
to Match Values¶
The following operation uses the $all
operator to query the
inventory
collection for documents where the value of the tags
field is an array whose elements include appliance
, school
, and
book
:
The above query returns the following documents:
Use $all
with $elemMatch
¶
If the field contains an array of documents, you can use the
$all
with the $elemMatch
operator.
The following operation queries the inventory
collection for
documents where the value of the qty
field is an array whose
elements match the $elemMatch
criteria:
The query returns the following documents:
The $all
operator exists to support queries on arrays. But
you may use the $all
operator to select against a non-array
field
, 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 the qty
field equals 50
.
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.