$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:
  1. { <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:

  1. { tags: { $all: [ "ssl" , "security" ] } }

is equivalent to:

  1. { $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]:

  1. db.articles.find( { tags: { $all: [ [ "ssl", "security" ] ] } } )

The query is equivalent to:

  1. db.articles.find( { $and: [ { tags: [ "ssl", "security" ] } ] } )

which is equivalent to:

  1. 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:

  1. tags: [ [ "ssl", "security" ], ... ]
  2. 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:

  1. {
  2. _id: ObjectId("5234cc89687ea597eabee675"),
  3. code: "xyz",
  4. tags: [ "school", "book", "bag", "headphone", "appliance" ],
  5. qty: [
  6. { size: "S", num: 10, color: "blue" },
  7. { size: "M", num: 45, color: "blue" },
  8. { size: "L", num: 100, color: "green" }
  9. ]
  10. }
  11.  
  12. {
  13. _id: ObjectId("5234cc8a687ea597eabee676"),
  14. code: "abc",
  15. tags: [ "appliance", "school", "book" ],
  16. qty: [
  17. { size: "6", num: 100, color: "green" },
  18. { size: "6", num: 50, color: "blue" },
  19. { size: "8", num: 100, color: "brown" }
  20. ]
  21. }
  22.  
  23. {
  24. _id: ObjectId("5234ccb7687ea597eabee677"),
  25. code: "efg",
  26. tags: [ "school", "book" ],
  27. qty: [
  28. { size: "S", num: 10, color: "blue" },
  29. { size: "M", num: 100, color: "blue" },
  30. { size: "L", num: 100, color: "green" }
  31. ]
  32. }
  33.  
  34. {
  35. _id: ObjectId("52350353b2eff1353b349de9"),
  36. code: "ijk",
  37. tags: [ "electronics", "school" ],
  38. qty: [
  39. { size: "M", num: 100, color: "green" }
  40. ]
  41. }

Use $all to Match Values

The following operation uses the $all operator to query theinventory collection for documents where the value of the tagsfield is an array whose elements include appliance, school, andbook:

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

The above query returns the following documents:

  1. {
  2. _id: ObjectId("5234cc89687ea597eabee675"),
  3. code: "xyz",
  4. tags: [ "school", "book", "bag", "headphone", "appliance" ],
  5. qty: [
  6. { size: "S", num: 10, color: "blue" },
  7. { size: "M", num: 45, color: "blue" },
  8. { size: "L", num: 100, color: "green" }
  9. ]
  10. }
  11.  
  12. {
  13. _id: ObjectId("5234cc8a687ea597eabee676"),
  14. code: "abc",
  15. tags: [ "appliance", "school", "book" ],
  16. qty: [
  17. { size: "6", num: 100, color: "green" },
  18. { size: "6", num: 50, color: "blue" },
  19. { size: "8", num: 100, color: "brown" }
  20. ]
  21. }

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:

  1. db.inventory.find( {
  2. qty: { $all: [
  3. { "$elemMatch" : { size: "M", num: { $gt: 50} } },
  4. { "$elemMatch" : { num : 100, color: "green" } }
  5. ] }
  6. } )

The query returns the following documents:

  1. {
  2. "_id" : ObjectId("5234ccb7687ea597eabee677"),
  3. "code" : "efg",
  4. "tags" : [ "school", "book"],
  5. "qty" : [
  6. { "size" : "S", "num" : 10, "color" : "blue" },
  7. { "size" : "M", "num" : 100, "color" : "blue" },
  8. { "size" : "L", "num" : 100, "color" : "green" }
  9. ]
  10. }
  11.  
  12. {
  13. "_id" : ObjectId("52350353b2eff1353b349de9"),
  14. "code" : "ijk",
  15. "tags" : [ "electronics", "school" ],
  16. "qty" : [
  17. { "size" : "M", "num" : 100, "color" : "green" }
  18. ]
  19. }

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:

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

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

  1. db.inventory.find( { "qty.num" : 50 } )

Both queries will select all documents in the inventorycollection 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

db.collection.find()