집계 프레임워크의 $unwind 단계는 입력 문서에서 배열 필드를 분해하여 각 요소에 대한 문서를 출력하는 데 사용됩니다. 각 출력 문서는 원본의 복사본이지만 배열 필드의 값이 단일 요소로 대체됩니다. 이는 배열에 저장된 데이터를 정규화하고 배열의 각 요소에 대해 개별적으로 작업을 수행하는 데 특히 유용합니다.
문법
{
$unwind: {
path: <field path>,
includeArrayIndex: <string>, // Optional
preserveNullAndEmptyArrays: <boolean> // Optional
}
}
매개 변수
| 매개 변수 | Description |
|---|---|
path |
배열 필드의 필드 경로입니다. 필수 매개 변수입니다. |
includeArrayIndex |
Optional. unwound 요소의 배열 인덱스를 보관할 새 필드의 이름입니다. |
preserveNullAndEmptyArrays |
Optional. 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: pomotion 이벤트 내에서 할인 해제
각 프로모션 이벤트 내 할인 정보 배열을 분해하고, 할인이 없는 문서를 보존하려면:
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에서 Azure DocumentDB로 마이그레이션하기 위한 옵션을 검토합니다.
- MongoDB와의 기능 호환성에 대해 자세히 알아보세요.