$facet
The `$facet` stage allows for multiple parallel aggregations to be executed within a single pipeline stage. It's useful for performing multiple analyses on the same dataset in a single query.
Syntax
{
$facet: {
"outputField1": [{ "stage1": {} }, { "stage2": {} }],
"outputField2": [{ "stage1": {} }, { "stage2": {} }]
}
}
Parameters
outputField
stringrequiredThe name of the output field for each parallel pipeline.
stage
objectrequiredThe aggregation stages to be executed in each pipeline.
Examples
Sample Data
{
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
"name": "First Up Consultants | Beverage Shop",
"sales": {
"salesByCategory": [
{ "categoryName": "Wine Accessories", "totalSales": 34440 }
]
},
"promotionEvents": [
{
"discounts": [
{ "categoryName": "Whiskey", "discountPercentage": 7 }
]
}
]
}
Faceted search on sales and promotions
Perform simultaneous analyses on sales and promotions for specified product categories.
This query runs two parallel pipelines analyzing sales totals and average discounts.
Query:
db.stores.aggregate([
{
$facet: {
salesAnalysis: [
{ $unwind: "$sales.salesByCategory" },
{ $match: { "sales.salesByCategory.categoryName": { $in: ["Laptops", "Smartphones"] } } },
{ $group: { _id: "$sales.salesByCategory.categoryName", totalSales: { $sum: "$sales.salesByCategory.totalSales" } } }
],
promotionAnalysis: [
{ $unwind: "$promotionEvents" },
{ $unwind: "$promotionEvents.discounts" },
{ $match: { "promotionEvents.discounts.categoryName": { $in: ["Laptops", "Smartphones"] } } },
{ $group: { _id: "$promotionEvents.discounts.categoryName", avgDiscount: { $avg: "$promotionEvents.discounts.discountPercentage" } } }
]
}
}
])
Output:
[
{
"salesAnalysis": [
{ "_id": "Smartphones", "totalSales": 440815 },
{ "_id": "Laptops", "totalSales": 679453 }
],
"promotionAnalysis": [
{ "_id": "Smartphones", "avgDiscount": 14.32 },
{ "_id": "Laptops", "avgDiscount": 14.78 }
]
}
]