Docs HomeMongoDB Manual

$all

$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.相反,当匹配单个元素时,“包含”表达式(即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$all$elemMatch一起使用

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集合中的文档,其中qty字段的值是元素匹配$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. But you may use the $all operator to select against a non-array field, as in the following example:$all运算符的存在是为了支持对数组的查询。但是,您可以使用$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. This operator provides a notable exception to this approach.在大多数情况下,MongoDB不将数组视为集合。此运算符为这种方法提供了一个明显的例外。

Additional Examples其他示例

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

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

Tip

See also: 另请参阅:

db.collection.find()