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> ... ] } }
$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" } ] }
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] | $all expression with a single element is for illustrative purposes since the $all expression is unnecessary if matching only a single element. $all 表达式用于说明目的,因为如果只匹配单个元素,则$all 表达式是不必要的。arrayField: element ) is more suitable.arrayField: element )更合适。 |
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" } ] }
$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
字段的值是一个数组,其元素包括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" } ] }
$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
的所有文档。
In most cases, MongoDB does not treat arrays as sets. 在大多数情况下,MongoDB不会将数组视为集合。This operator provides a notable exception to this approach.该运算符为这种方法提供了一个显著的例外。
For additional examples in querying arrays, see:有关查询数组的其他示例,请参阅:
For additional examples in querying, see:有关查询的其他示例,请参阅: