$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
运营
$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 [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 )更合适。 |
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
匹配值
$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 $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
的所有文档。
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:有关查询的其他示例,请参阅: