$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. $all运算符选择字段值为包含所有指定元素的数组的文档。To specify an $all expression, use the following prototype:要指定$all表达式,请使用以下原型:

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

Behavior行为

Equivalent to $and Operation相当于$and操作

The $all is equivalent to an $and operation of the specified values; i.e. the following statement:$all相当于指定值的$and运算;即以下声明:

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

is equivalent to:等于:

{ $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" ]).当传递一个嵌套数组的数组(例如[ [ "A" ] ])时,$all将匹配其中字段包含嵌套数组作为元素的文档(例如field: [ [ "A" ], ... ]),或者字段等于嵌套数组(例如field: [ "A" ])。

For example, consider the following query [1]:例如,考虑以下查询[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 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:因此,$all表达式与tags字段是包含嵌套数组[ "ssl", "security" ]的数组或等于嵌套数组的数组的文档相匹配:

tags: [ [ "ssl", "security" ], ... ]
tags: [ "ssl", "security" ]
[1] The $all expression with a single element is for illustrative purposes since the $all expression is unnecessary if matching only a single element. 带有单个元素的$all表达式用于说明目的,因为如果只匹配单个元素,则$all表达式是不必要的。Instead, when matching a single element, a "contains" expression (i.e. arrayField: element ) is more suitable.相反,当匹配单个元素时,“contains”表达式(即arrayField: element)更合适。

Examples示例

The following examples use the inventory collection that contains the documents:以下示例使用包含文档的inventory集合:

{
   _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使用$all匹配值

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:以下操作使用$all运算符查询inventory集合中的文档,其中tags字段的值是一个数组,其元素包括applianceschoolbook

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$elemMatch中使用$all

If the field contains an array of documents, you can use the $all with the $elemMatch operator.如果字段包含一个文档数组,则可以将$all$elemMatch运算符一起使用。

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:以下操作查询inventory集合中的文档,其中数量字段的值是其元素与$elemMatch条件匹配的数组:

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. $all运算符用于支持数组查询。But you may use the $all operator to select against a non-array field, as in the following example:但您可以使用$all运算符对非数组field进行选择,如下例所示:

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.这两个查询都将选择inventory集合中num字段值等于50的所有文档。

Note注意

In most cases, MongoDB does not treat arrays as sets. 在大多数情况下,MongoDB不会将数组视为集合。This operator provides a notable exception to this approach.该运算符为这种方法提供了一个显著的例外。

Additional Examples更多示例

For additional examples in querying arrays, see:有关查询数组的其他示例,请参阅:

For additional examples in querying, see:有关查询的其他示例,请参阅:

Tip提示
See also: 参阅:
←  Array Query Operators$elemMatch (query) →