$all¶
-
$all¶ Syntax:
{ field: { $all: [ <value> , <value1> ... ] }$allselects the documents where thefieldholds 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
inventorycollection where thetagsfield contains an array with the elements,appliances,school, andbook.Therefore, the above query will match documents in the
inventorycollection that have atagsfield that hold either of the following arrays:The
$alloperator exists to describe and specify arrays in MongoDB queries. However, you may use the$alloperator 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
inventorycollection where the value of theqtyfield 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
$alloperator 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.