你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
$all 运算符用于选择字段的值是包含所有指定元素的数组的文档。 如果需要确保数组字段包含多个指定的元素,而不考虑数组中的顺序,则此运算符非常有用。
Syntax
db.collection.find({
field : {
$all: [ < value1 > , < value2 > ]
}
})
参数
| 参数 | Description |
|---|---|
field |
要查询的字段。 |
<value1> , <value2> |
数组字段中必须全部存在的值。 |
例子
请考虑商店集合中的这个示例文档。
{
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
"location": {
"lat": -89.2384,
"lon": -46.4012
},
"staff": {
"totalStaff": {
"fullTime": 8,
"partTime": 20
}
},
"sales": {
"totalSales": 75670,
"salesByCategory": [
{
"categoryName": "Wine Accessories",
"totalSales": 34440
},
{
"categoryName": "Bitters",
"totalSales": 39496
},
{
"categoryName": "Rum",
"totalSales": 1734
}
]
},
"promotionEvents": [
{
"eventName": "Unbeatable Bargain Bash",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 6,
"Day": 23
},
"endDate": {
"Year": 2024,
"Month": 7,
"Day": 2
}
},
"discounts": [
{
"categoryName": "Whiskey",
"discountPercentage": 7
},
{
"categoryName": "Bitters",
"discountPercentage": 15
},
{
"categoryName": "Brandy",
"discountPercentage": 8
},
{
"categoryName": "Sports Drinks",
"discountPercentage": 22
},
{
"categoryName": "Vodka",
"discountPercentage": 19
}
]
},
{
"eventName": "Steal of a Deal Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 9,
"Day": 21
},
"endDate": {
"Year": 2024,
"Month": 9,
"Day": 29
}
},
"discounts": [
{
"categoryName": "Organic Wine",
"discountPercentage": 19
},
{
"categoryName": "White Wine",
"discountPercentage": 20
},
{
"categoryName": "Sparkling Wine",
"discountPercentage": 19
},
{
"categoryName": "Whiskey",
"discountPercentage": 17
},
{
"categoryName": "Vodka",
"discountPercentage": 23
}
]
}
]
}
示例 1:查找包含数组中所有指定元素的文档
此查询检索包含元素 Laptops 和 Smartphones 数组中的 salesByCategory.categoryName 文档。
db.stores.find({
"sales.salesByCategory.categoryName": {
$all: ["Laptops", "Smartphones"]
}
}, {
_id: 1,
"sales.salesByCategory.categoryName": 1
}).limit(2)
此查询返回的前两个结果为:
[
{
"_id": "a57511bb-1ea3-4b26-bf0d-8bf928f2bfa8",
"sales": {
"salesByCategory": [
{
"categoryName": "Smartphones"
},
{
"categoryName": "Laptops"
}
]
}
},
{
"_id": "ca56d696-5208-40c3-aa04-d4e245df44dd",
"sales": {
"salesByCategory": [
{
"categoryName": "Laptops"
},
{
"categoryName": "Smartphones"
}
]
}
}
]
相关内容
- 查看用于 从 MongoDB 迁移到 Azure DocumentDB 的选项。
- 详细了解 与 MongoDB 的功能兼容性。