你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
$elemMatch 运算符用于匹配包含至少一个与所有指定查询条件匹配的元素的数组字段的文档。 当需要查找具有指定元素的数组文档时,此运算符非常有用。
Syntax
db.collection.find({ <field>: { $elemMatch: { <query1>, <query2>, ... } } })
参数
| 参数 | Description |
|---|---|
field |
文档中包含要查询的数组的字段。 |
query |
数组中至少有一个元素必须满足的条件。 |
例子
请考虑商店集合中的这个示例文档。
{
"_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:在数组中查找元素列表中的特定元素
此查询检索集合中至少有一个折扣且类别名称为“DJ 照明”stores的集合中的promotionEvents前两个文档。 查询仅返回这些文档的 _id 和 promotionEvents.discounts 字段。
db.stores.find({
"promotionEvents.discounts": {
$elemMatch: {
categoryName: "DJ Lighting"
}
}
}, {
_id: 1,
"promotionEvents.discounts": 1
}).limit(2)
此查询返回以下结果:
[
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"promotionEvents": [
{
"discounts": [
{
"categoryName": "DJ Turntables",
"discountPercentage": 18
},
{
"categoryName": "DJ Mixers",
"discountPercentage": 15
}
]
},
{
"discounts": [
{
"categoryName": "DJ Lighting",
"discountPercentage": 14
},
{
"categoryName": "DJ Cases",
"discountPercentage": 20
}
]
}
]
},
{
"_id": "91de5201-8194-44bf-848f-674e8df8bf5e",
"promotionEvents": [
{
"discounts": [
{
"categoryName": "DJ Cases",
"discountPercentage": 6
},
{
"categoryName": "DJ Mixers",
"discountPercentage": 14
}
]
},
{
"discounts": [
{
"categoryName": "DJ Headphones",
"discountPercentage": 19
},
{
"categoryName": "DJ Speakers",
"discountPercentage": 13
}
]
},
{
"discounts": [
{
"categoryName": "DJ Lighting",
"discountPercentage": 12
},
{
"categoryName": "DJ Accessories",
"discountPercentage": 6
}
]
}
]
}
]
相关内容
- 查看用于 从 MongoDB 迁移到 Azure DocumentDB 的选项。
- 详细了解 与 MongoDB 的功能兼容性。