Navigation
This version of the documentation is archived and no longer supported.

$all

$all

Syntax: { field: { $all: [ <value> , <value1> ... ] }

$all selects the documents where the field holds an array and contains all elements (e.g. <value>, <value1>, etc.) in the array.

Consider the following example:

db.inventory.find( { tags: { $all: [ "appliances", "school", "book" ] } } )

This query selects all documents in the inventory collection where the tags field contains an array with the elements, appliances, school, and book.

Therefore, the above query will match documents in the inventory collection that have a tags field that hold either of the following arrays:

[ "school", "book", "bag", "headphone", "appliances" ]
[ "appliances", "school", "book" ]

The $all operator exists to describe and specify arrays in MongoDB queries. However, you may use the $all operator to select against a non-array field, as in the following example:

db.inventory.find( { qty: { $all: [ 50 ] } } )

However, use the following form to express the same query:

db.inventory.find( { qty: 50 } )

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.

See also

find(), update(), and $set.