$all
$all
- The
$all
operator selects the documents where the value ofa field is an array that contains all the specified elements. Tospecify an$all
expression, use the following prototype:
- { <field>: { $all: [ <value1> , <value2> ... ] } }
Behavior
Equivalent to $and Operation
Changed in version 2.6.
The $all
is equivalent to an $and
operation of thespecified values; i.e. the following statement:
- { tags: { $all: [ "ssl" , "security" ] } }
is equivalent to:
- { $and: [ { tags: "ssl" }, { tags: "security" } ] }
Nested Array
Changed in version 2.6.
When passed an array of a nested array (e.g. [ [ "A" ] ]
),$all
can now match documents where the field contains thenested array as an element (e.g. field: [ [ "A" ], … ]
), or thefield equals the nested array (e.g. field: [ "A" ]
).
For example, consider the following query [1]:
- 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 can match documents where thetags
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" ]
This behavior for $all
allows for more matches than previousversions of MongoDB. Earlier versions could only match documents wherethe field contains the nested array.
[1] | The $all expression with a single element is forillustrative purposes since the $all expression isunnecessary if matching only a single element. Instead, whenmatching a single element, a “contains” expression (i.e.arrayField: element ) is more suitable. |
Examples
The following examples use the inventory
collection that containsthe documents:
- {
- _id: ObjectId("5234cc89687ea597eabee675"),
- code: "xyz",
- tags: [ "school", "book", "bag", "headphone", "appliance" ],
- qty: [
- { size: "S", num: 10, color: "blue" },
- { size: "M", num: 45, color: "blue" },
- { size: "L", num: 100, color: "green" }
- ]
- }
- {
- _id: ObjectId("5234cc8a687ea597eabee676"),
- code: "abc",
- tags: [ "appliance", "school", "book" ],
- qty: [
- { size: "6", num: 100, color: "green" },
- { size: "6", num: 50, color: "blue" },
- { size: "8", num: 100, color: "brown" }
- ]
- }
- {
- _id: ObjectId("5234ccb7687ea597eabee677"),
- code: "efg",
- tags: [ "school", "book" ],
- qty: [
- { size: "S", num: 10, color: "blue" },
- { size: "M", num: 100, color: "blue" },
- { size: "L", num: 100, color: "green" }
- ]
- }
- {
- _id: ObjectId("52350353b2eff1353b349de9"),
- code: "ijk",
- tags: [ "electronics", "school" ],
- qty: [
- { size: "M", num: 100, color: "green" }
- ]
- }
Use $all to Match Values
The following operation uses the $all
operator to query theinventory
collection for documents where the value of the tags
field is an array whose elements include appliance
, school
, andbook
:
- db.inventory.find( { tags: { $all: [ "appliance", "school", "book" ] } } )
The above query returns the following documents:
- {
- _id: ObjectId("5234cc89687ea597eabee675"),
- code: "xyz",
- tags: [ "school", "book", "bag", "headphone", "appliance" ],
- qty: [
- { size: "S", num: 10, color: "blue" },
- { size: "M", num: 45, color: "blue" },
- { size: "L", num: 100, color: "green" }
- ]
- }
- {
- _id: ObjectId("5234cc8a687ea597eabee676"),
- code: "abc",
- tags: [ "appliance", "school", "book" ],
- qty: [
- { size: "6", num: 100, color: "green" },
- { size: "6", num: 50, color: "blue" },
- { size: "8", num: 100, color: "brown" }
- ]
- }
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 fordocuments where the value of the qty
field is an array whoseelements match the $elemMatch
criteria:
- db.inventory.find( {
- qty: { $all: [
- { "$elemMatch" : { size: "M", num: { $gt: 50} } },
- { "$elemMatch" : { num : 100, color: "green" } }
- ] }
- } )
The query returns the following documents:
- {
- "_id" : ObjectId("5234ccb7687ea597eabee677"),
- "code" : "efg",
- "tags" : [ "school", "book"],
- "qty" : [
- { "size" : "S", "num" : 10, "color" : "blue" },
- { "size" : "M", "num" : 100, "color" : "blue" },
- { "size" : "L", "num" : 100, "color" : "green" }
- ]
- }
- {
- "_id" : ObjectId("52350353b2eff1353b349de9"),
- "code" : "ijk",
- "tags" : [ "electronics", "school" ],
- "qty" : [
- { "size" : "M", "num" : 100, "color" : "green" }
- ]
- }
The $all
operator exists to support queries on arrays. Butyou may use the $all
operator to select against a non-arrayfield
, as in the following example:
- db.inventory.find( { "qty.num": { $all: [ 50 ] } } )
However, use the following form to express the same query:
- db.inventory.find( { "qty.num" : 50 } )
Both queries will select all documents in the inventory
collection where the value of the num
field equals 50
.
Note
In most cases, MongoDB does not treat arrays as sets. This operatorprovides a notable exception to this approach.
Additional Examples
For additional examples in querying arrays, see:
For additional examples in querying, see:
See also