On this page
$all
在本页面
{ <field>: { $all: [ <value1> , <value2> ... ] } }
Behavior
等效于$ and 操作
在 2.6 版中进行了更改。
{ tags: { $all: [ "ssl" , "security" ] } }
等效于:
{ $and: [ { tags: "ssl" }, { tags: "security" } ] }
Nested Array
在 2.6 版中进行了更改。
当传递嵌套数组的数组(例如[ [ "A" ] ]
)时,$all现在可以匹配文档,其中该字段包含嵌套数组作为元素(例如field: [ [ "A" ], ... ]
),或该字段等于嵌套数组(例如field: [ "A" ]
)。
例如,考虑以下查询[1]:
db.articles.find( { tags: { $all: [ [ "ssl", "security" ] ] } } )
该查询等效于:
db.articles.find( { $and: [ { tags: [ "ssl", "security" ] } ] } )
等效于:
db.articles.find( { tags: [ "ssl", "security" ] } )
这样,$all表达式可以匹配文档,其中tags
字段是包含嵌套数组[ "ssl", "security" ]
的数组,或者是等于嵌套数组的数组:
tags: [ [ "ssl", "security" ], ... ]
tags: [ "ssl", "security" ]
$all的这种行为比以前的 MongoDB 版本具有更多的匹配项。较早的版本只能与字段包含嵌套数组的文档匹配。
Examples
以下示例使用包含文档的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 匹配值
以下操作使用$all运算符查询inventory
集合以获取文档,其中tags
字段的值是一个数组,其元素包括appliance
,school
和book
:
db.inventory.find( { tags: { $all: [ "appliance", "school", "book" ] } } )
上面的查询返回以下文档:
{
_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 与$ elemMatch 一起使用
如果该字段包含文档数组,则可以将$all与$elemMatch运算符一起使用。
以下操作在inventory
集合中查询文档,其中qty
字段的值是一个数组,其元素与$elemMatch条件匹配:
db.inventory.find( {
qty: { $all: [
{ "$elemMatch" : { size: "M", num: { $gt: 50} } },
{ "$elemMatch" : { num : 100, color: "green" } }
] }
} )
该查询返回以下文档:
{
"_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运算符以支持对数组的查询。但是您可以使用$all运算符针对非数组field
进行选择,如以下示例所示:
db.inventory.find( { "qty.num": { $all: [ 50 ] } } )
但是 ,请使用以下形式表示相同的查询:
db.inventory.find( { "qty.num" : 50 } )
这两个查询都将选择inventory
集合中num
字段的值等于50
的所有文档。
Note
在大多数情况下,MongoDB 不会将数组视为集合。该运算符对此方法提供了明显的 exception。
Additional Examples
有关查询数组的其他示例,请参见:
有关查询的其他示例,请参见:
See also