Not
Bu sayfaya erişim yetkilendirme gerektiriyor. Oturum açmayı veya dizinleri değiştirmeyi deneyebilirsiniz.
Bu sayfaya erişim yetkilendirme gerektiriyor. Dizinleri değiştirmeyi deneyebilirsiniz.
Toplama çerçevesindeki $unwind aşaması, giriş belgelerinden bir dizi alanını her öğe için bir belgenin çıktısını almak üzere yok etmek için kullanılır. Her çıkış belgesi özgün belgenin bir kopyasıdır, ancak dizi alanının değeri tek bir öğeyle değiştirilir. Bu özellikle dizilerde depolanan verileri normalleştirmek ve bir dizinin her öğesi üzerinde ayrı ayrı işlemler gerçekleştirmek için kullanışlıdır.
Sözdizimi
{
$unwind: {
path: <field path>,
includeArrayIndex: <string>, // Optional
preserveNullAndEmptyArrays: <boolean> // Optional
}
}
Parametreler
| Parametre | Description |
|---|---|
path |
Bir dizi alanının alan yolu. Bu gerekli bir parametredir. |
includeArrayIndex |
Optional. Unwound öğesinin dizi dizinini tutacak yeni bir alanın adı. |
preserveNullAndEmptyArrays |
Optional. True ise, yol null, eksik veya boş bir diziyse, $unwind belgenin çıkışını değişmeden verir. |
Örnekler
Stores koleksiyonundaki bu örnek belgeyi göz önünde bulundurun.
{
"_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
}
]
}
]
}
Örnek 1: Satışları kategoriye göre geri sarma
Mağaza belgesindeki salesByCategory dizisini yapılandırmak için:
db.stores.aggregate([
{
$unwind: "$sales.salesByCategory"
}
])
Bu sorgu tarafından döndürülen ilk iki sonuç şunlardır:
[
{
"_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
}
}
}
}
]
Örnek 2: Dizi diziniyle yükseltme olaylarını geri alma
promotionEvents dizisini yapılandırmak ve dizi dizinini çıkışa eklemek için:
db.stores.aggregate([
{
$unwind: {
path: "$promotionEvents",
includeArrayIndex: "eventIndex"
}
}
])
Bu sorgu tarafından döndürülen ilk iki sonuç şunlardır:
[
{
"_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
}
}
]
Örnek 3: Pomotion olayları içindeki indirimleri geri alma
Her promosyon olayındaki indirim dizisini çözmek ve indirim içermeyen belgeleri korumak için:
db.stores.aggregate([
{
$unwind: {
path: "$promotionEvents.discounts",
preserveNullAndEmptyArrays: true
}
}
])
Bu sorgu tarafından döndürülen ilk iki sonuç şunlardır:
[
{
"_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"
}
}
}
]