$allThe$alloperator selects the documents where the value of a field matches all specified values. The matched documents can either contain a field with a value that is an array containing all the specified elements, or a field with a single value matching the specified element.$all运算符选择字段值与所有指定值匹配的文档。匹配的文档可以包含一个具有值的字段,该值是一个包含所有指定元素的数组,也可以包含具有与指定元素匹配的单个值的字段。
Compatibility兼容性
You can use 您可以将$all for deployments hosted in the following environments:$all用于在以下环境中托管的部署:
- MongoDB Atlas
: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
- MongoDB Enterprise
: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本 - MongoDB Community
: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本
Syntax语法
To specify an 要指定$all expression, use the following prototype:$all表达式,请使用以下原型:
{ <field>: { $all: [ <value1> , <value2> ... ] } }Behavior行为
Equivalent to $and Operation相当于$and操作
$and OperationThe $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:例如,考虑以下查询:
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" ]Empty Array空数组
When passed an empty array, 当传递一个空数组时,$all matches no documents.$all不匹配任何文档。
Examples示例
The exampless in this section use the 本节中的示例使用包含以下文档的inventory collection that contains the following documents:inventory集合:
db.inventory.insertMany ( [
{
_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匹配值
$all to Match ValuesThe 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字段的值是一个数组,其元素包括appliance、school和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将$all与$elemMatch一起使用
$all with $elemMatchIf 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运算符对非数组字段进行选择,如下例所示:
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 on querying arrays, see:有关查询数组的其他示例,请参阅:
For additional examples on querying, see 有关查询的其他示例,请参阅查询文档。Query Documents.查询文档。