翻译或纠错本页面

$all

On this page

$all

The $all operator selects the documents where the value of a field is an array that contains all the specified elements. To specify an $all expression, use the following prototype:

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

Behavior

Equivalent to $and Operation

在 2.6 版更改.

The $all is equivalent to an $and operation of the specified values; i.e. the following statement:

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

is equivalent to:

{ $and: [ { tags: "ssl" }, { tags: "security" } ] }

Nested Array

在 2.6 版更改.

When passed an array of a nested array (e.g. [ [ "A" ] ] ), $all can now match 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 [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 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" ]

This behavior for $all allows for more matches than previous versions of MongoDB. Earlier versions could only match documents where the field contains the nested array.

[1]The $all expression with a single element is for illustrative purposes since the $all expression is unnecessary if matching only a single element. Instead, when matching a single element, a “contains” expression (i.e. arrayField: element ) is more suitable.

Examples

The following examples use the inventory collection that contains the 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 the inventory collection for documents where the value of the tags field is an array whose elements include appliance, school, and book:

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 for documents where the value of the qty field is an array whose elements 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. But you may use the $all operator to select against a non-array field, 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.

注解

In most cases, MongoDB does not treat arrays as sets. This operator provides a notable exception to this approach.

参见

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