你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
阶段 $facet 聚合管道允许在单个管道阶段内执行多个并行聚合。 它可用于在单个查询中对同一数据集执行多个分析。
Syntax
{
"$facet": {
"outputField1": [ { "stage1": {} }, { "stage2": {} } ],
"outputField2": [ { "stage1": {} }, { "stage2": {} } ]
}
}
参数
| 参数 | Description |
|---|---|
outputFieldN |
输出字段的名称。 |
stageN |
要执行的聚合阶段。 |
例子
请考虑商店集合中的这个示例文档。
{
"_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:对销售和促销进行分面搜索
针对指定的产品类别对销售和促销执行同时分析。 管道 salesAnalysis 展开某些类别的 salesByCategory筛选器,并将其分组以求和 totalSales。 促销分析管道展开促销事件及其折扣、筛选特定类别(如LaptopsSmartphones等)并对其进行分组以计算平均折扣百分比。 从集合中提取的输入文档 stores 仅在此操作开始时从数据库提取一次。
db.stores.aggregate([
{
$facet: {
salesAnalysis: [
{ $unwind: "$sales.salesByCategory" },
{ $match: { "sales.salesByCategory.categoryName": { $in: ["Laptops", "Smartphones", "Cameras", "Watches"] } } },
{ $group: { _id: "$sales.salesByCategory.categoryName", totalSales: { $sum: "$sales.salesByCategory.totalSales" } } }
],
promotionAnalysis: [
{ $unwind: "$promotionEvents" },
{ $unwind: "$promotionEvents.discounts" },
{ $match: { "promotionEvents.discounts.categoryName": { $in: ["Laptops", "Smartphones", "Cameras", "Watches"] } } },
{ $group: { _id: "$promotionEvents.discounts.categoryName", avgDiscount: { $avg: "$promotionEvents.discounts.discountPercentage" } } }
]
}
}
]).pretty()
此查询返回以下结果:
[
{
"salesAnalysis": [
{ "_id": "Smartphones", "totalSales": 440815 },
{ "_id": "Laptops", "totalSales": 679453 },
{ "_id": "Cameras", "totalSales": 481171 },
{ "_id": "Watches", "totalSales": 492299 }
],
"promotionAnalysis": [
{ "_id": "Smartphones", "avgDiscount": 14.32 },
{ "_id": "Laptops", "avgDiscount": 14.780645161290323 },
{ "_id": "Cameras", "avgDiscount": 15.512195121951219 },
{ "_id": "Watches", "avgDiscount": 15.174418604651162 }
]
}
]
相关内容
- 查看用于 从 MongoDB 迁移到 Azure DocumentDB 的选项。
- 详细了解 与 MongoDB 的功能兼容性。