聚合框架中的 $unwind 阶段用于从输入文档解构数组字段,以便为每个元素输出文档。 每个输出文档都是原始文档的副本,但数组字段的值被单个元素替换。 这对于规范化存储在数组中的数据以及单独对数组的每个元素执行操作尤其有用。
语法
{
$unwind: {
path: <field path>,
includeArrayIndex: <string>, // Optional
preserveNullAndEmptyArrays: <boolean> // Optional
}
}
参数
参数 | DESCRIPTION |
---|---|
path |
数组字段的字段路径。 这是必需参数。 |
includeArrayIndex |
可选。 要保存未展开元素的数组索引的新字段的名称。 |
preserveNullAndEmptyArrays |
可选。 如果此项为 true,且如果路径为 null、缺失或空数组,则 $unwind 输出的文档不变。 |
例子
请考虑商店集合中的这个示例文档。
{
"_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:按类别展开销售额
解构商店文档中的 salesByCategory 数组:
db.stores.aggregate([
{
$unwind: "$sales.salesByCategory"
}
])
此查询返回的前两个结果为:
[
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"store": {
"name": "Downtown Store",
"sales": {
"totalSales": 15000,
"salesByCategory": {
"category": "Electronics",
"totalSales": 5000
}
}
}
},
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"store": {
"name": "Downtown Store",
"sales": {
"totalSales": 15000,
"salesByCategory": {
"category": "Clothing",
"totalSales": 10000
}
}
}
}
]
示例 2:使用数组索引展开升级事件
解构 promotionEvents 数组,并在输出中包含数组索引:
db.stores.aggregate([
{
$unwind: {
path: "$promotionEvents",
includeArrayIndex: "eventIndex"
}
}
])
此查询返回的前两个结果为:
[
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"store": {
"name": "Downtown Store",
"promotionEvents": {
"eventName": "Summer Sale",
"eventDate": ISODate("2024-08-01T00:00:00Z")
},
"eventIndex": 0
}
},
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"store": {
"name": "Downtown Store",
"promotionEvents": {
"eventName": "Black Friday",
"eventDate": ISODate("2024-11-25T00:00:00Z")
},
"eventIndex": 1
}
}
]
示例 3:在推车事件中展开折扣
在每个促销活动中解构折扣数组,并保留没有折扣的文档:
db.stores.aggregate([
{
$unwind: {
path: "$promotionEvents.discounts",
preserveNullAndEmptyArrays: true
}
}
])
此查询返回的前两个结果为:
[
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"store": {
"name": "Downtown Store",
"promotionEvents": {
"eventName": "Summer Sale",
"discounts": {
"discountType": "Percentage",
"discountAmount": 20
}
}
}
},
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"store": {
"name": "Downtown Store",
"promotionEvents": {
"eventName": "Black Friday"
}
}
}
]
相关内容
- 查看有关 从 MongoDB 迁移到适用于 MongoDB 的 Azure Cosmos DB (vCore) 的选项。
- 详细了解 与 MongoDB 的功能兼容性。