$allThe
$alloperator selects the documents where the value of a field matches all specified values. The matched documents can either contain a field with a value that is an array containing all the specified elements, or a field with a single value matching the specified element.
Compatibility
You can use $all for deployments hosted in the following
environments:
MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
MongoDB Enterprise: The subscription-based, self-managed version of MongoDB
MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB
Syntax
To specify an $all expression, use the following prototype:
{ <field>: { $all: [ <value1> , <value2> ... ] } }
Behavior
Equivalent to $and Operation
$all is equivalent to an $and operation of the
specified values. For example, the following two queries are equivalent:
{ tags: { $all: [ "ssl" , "security" ] } } { $and: [ { tags: "ssl" }, { tags: "security" } ] }
Nested Array
When passed an array of a nested array (e.g. [ [ "A" ] ] ),
$all matches documents where the field contains the
nested array as an element (e.g. field: [ [ "A" ], ... ]), or the
field equals the nested array (e.g. field: [ "A" ]).
For example, consider the following query:
db.articles.find( { tags: { $all: [ [ "ssl", "security" ] ] } } )
The query is equivalent to:
db.articles.find( { $and: [ { tags: [ "ssl", "security" ] } ] } )
which is equivalent to:
db.articles.find( { tags: [ "ssl", "security" ] } )
As such, the $all expression matches documents where the
tags field is an array that contains the nested array [ "ssl",
"security" ] or is an array that equals the nested array:
tags: [ [ "ssl", "security" ], ... ] tags: [ "ssl", "security" ]
Empty Array
When passed an empty array, $all matches no documents.
Examples
The examples on this page use data from the sample_mflix sample dataset. For details on how to load this dataset into your self-managed MongoDB deployment, see Load the sample dataset. If you made any modifications to the sample databases, you may need to drop and recreate the databases to run the examples on this page.
Use $all to Match Values
The following operation uses the $all operator to query the
movies collection for documents where the value of the directors
field is an array whose elements include John Murray Anderson and Pèl Fejès:
db.movies.find( { directors: { $all: ['John Murray Anderson','Pèl Fejès'] } }, { title: 1, directors: 1, year: 1 })
The above query returns the following document:
[ { _id: ObjectId('573a1391f29313caabcd9336'), title: 'King of Jazz', directors: [ 'John Murray Anderson', 'Pèl Fejès' ], year: 1930 } ]
Use $all with $elemMatch
To match multiple conditions on a single array element, or against an array of
documents, you can use $all with the $elemMatch operator.
The following operation queries the
embedded_movies collection for documents where:
At least one writer has a
storycredit but not ascreenplaycreditAt least one writer has a
titlescredit but not anadaptationcredit
db.embedded_movies.find( { writers: { $all: [ { $elemMatch: { $regex: '\\bstory\\b', $not: { $regex: '\\bscreenplay\\b' }}}, { $elemMatch: { $regex: '\\btitles\\b', $not: { $regex: '\\badaptation\\b' }}} ]}}, { title: 1, writers: 1, year: 1 })
The query returns the following documents:
[ { "_id": { "$oid": "573a1391f29313caabcd93a3" }, "title": "Men Without Women", "writers": [ "John Ford (story)", "James Kevin McGuinness (story)", "Dudley Nichols (screen play and scenario)", "Otis C. Freeman (titles)" ], "year": 1930 }, { "_id": { "$oid": "573a1391f29313caabcd8319" }, "title": "For Heaven's Sake", "writers": [ "Ted Wilde (story)", "John Grey (story)", "Clyde Bruckman (story)", "Ralph Spence (titles)" ], "year": 1926 }, { "_id": { "$oid": "573a1391f29313caabcd7bc3" }, "title": "The Iron Horse", "writers": [ "Charles Kenyon (story)", "John Russell (story)", "Charles Kenyon (scenario)", "Charles Darnton (titles)" ], "year": 1924 } ]
Note
In most cases, MongoDB does not treat arrays as sets. This operator provides a notable exception to this approach.
Additional Examples
For additional examples on querying arrays, see:
For additional examples on querying, see Query Documents.