Aracılığıyla paylaş


$slice

işleci $slice , bir dizinin alt kümesini döndürmek için kullanılır. Bir dizideki öğe sayısını belirtilen bir sayıyla sınırlamak veya dizideki belirtilen bir konumdan öğe döndürmek için kullanılabilir. işleç, işleme veya görüntüleme için verilerin yalnızca bir kısmının gerekli olduğu büyük dizilerle ilgilenirken kullanışlıdır.

Sözdizimi

İşlecin $slice söz dizimi aşağıdaki gibidir.

  • Dizinin başındaki veya sonundaki öğeleri döndürür
{
  $slice: [ <array>, <n> ]
}
  • Dizide belirtilen konumdan öğeleri döndürür
{
  $slice: [ <array>, <position>, <n> ]
}

Parametreler

Parametre Description
array Geçerli bir dizi ifadesi.
position Geçerli bir tamsayı ifadesi.
n Geçerli bir tamsayı ifadesi.

Ö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: Diziden öğe kümesini döndürme

Bu sorgu koleksiyon içinde sales.salesByCategory için _id: 988d2dd1-2faa-4072-b420-b91b95cbfd60 dizinin ilk üç öğesini stores alır.

db.stores.aggregate([{
    $match: {
        _id: "988d2dd1-2faa-4072-b420-b91b95cbfd60"
    }
}, {
    $project: {
        salesByCategory: {
            $slice: ["$sales.salesByCategory", 3]
        }
    }
}])

Bu sorgu aşağıdaki sonucu döndürür.

[
    {
        "_id": "988d2dd1-2faa-4072-b420-b91b95cbfd60",
        "salesByCategory": [
            {
                "categoryName": "Towel Racks",
                "totalSales": 13237
            },
            {
                "categoryName": "Washcloths",
                "totalSales": 44315
            },
            {
                "categoryName": "Face Towels",
                "totalSales": 42095
            }
        ]
    }
]

Örnek 2: $push ile dilimleyin

Bu sorgu ile işlevini $push kullanarak $each diziye promotionEvents yeni öğeler ekler ve $slice yalnızca ilk N (pozitif dilim) veya son N (negatif dilim) öğelerini korur. Bu, dizinin güncelleştirmeden sonra en son girişlerin kalmasını sağlar.

db.stores.updateOne({
    _id: "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5"
}, {
    $push: {
        promotionEvents: {
            $each: [{
                    eventName: "Black Friday Event",
                    promotionalDates: {
                        startDate: {
                            Year: 2024,
                            Month: 8,
                            Day: 1
                        },
                        endDate: {
                            Year: 2024,
                            Month: 8,
                            Day: 7
                        }
                    },
                    discounts: [{
                        categoryName: 'DJ Speakers',
                        discountPercentage: 25
                    }]
                },
                {
                    eventName: "Mega Discount Days",
                    promotionalDates: {
                        startDate: {
                            Year: 2024,
                            Month: 5,
                            Day: 11
                        },
                        endDate: {
                            Year: 2024,
                            Month: 5,
                            Day: 18
                        }
                    },
                    discounts: [{
                        categoryName: "DJ Lights",
                        discountPercentage: 20
                    }]
                }
            ],
            $slice: -3
        }
    }
})

Sorgu aşağıdaki sonucu döndürür.

[
    {
        "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
        "name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile",
        "location": {
            "lat": 60.1441,
            "lon": -141.5012
        },
        "staff": {
            "totalStaff": {
                "fullTime": 2,
                "partTime": 0
            }
        },
        "sales": {
            "salesByCategory": [
                {
                    "categoryName": "DJ Headphones",
                    "totalSales": 35921
                },
                {
                    "categoryName": "DJ Cables",
                    "totalSales": 1000
                }
            ],
            "fullSales": 3700
        },
        "promotionEvents": [
            {
                "eventName": "Cyber Monday Event",
                "promotionalDates": {
                    "startDate": {
                        "Year": 2024,
                        "Month": 8,
                        "Day": 1
                    },
                    "endDate": {
                        "Year": 2024,
                        "Month": 8,
                        "Day": 7
                    }
                },
                "discounts": [
                    {
                        "categoryName": "DJ Speakers",
                        "discountPercentage": 25
                    }
                ]
            },
            {
                "eventName": "Black Friday Event",
                "promotionalDates": {
                    "startDate": {
                        "Year": 2024,
                        "Month": 8,
                        "Day": 1
                    },
                    "endDate": {
                        "Year": 2024,
                        "Month": 8,
                        "Day": 7
                    }
                },
                "discounts": [
                    {
                        "categoryName": "DJ Speakers",
                        "discountPercentage": 25
                    }
                ]
            },
            {
                "eventName": "Mega Discount Days",
                "promotionalDates": {
                    "startDate": {
                        "Year": 2024,
                        "Month": 5,
                        "Day": 11
                    },
                    "endDate": {
                        "Year": 2024,
                        "Month": 5,
                        "Day": 18
                    }
                },
                "discounts": [
                    {
                        "categoryName": "DJ Lights",
                        "discountPercentage": 20
                    }
                ]
            }
        ],
        "tag": [
            "#ShopLocal",
            "#NewArrival",
            "#FashionStore",
            "#SeasonalSale",
            "#FreeShipping",
            "#MembershipDeals"
        ]
    }
]

Örnek 3: Diziden eşleşen ilk öğeyi getirme

Bu sorgu ilk belgeyi "sales.salesByCategory" dizisinden alır.

db.stores.find({
        name: "Lakeshore Retail"
    }, {
        _id: 1,
        name: 1,
        "sales.salesByCategory": {
            $slice: 1
        }
    } // restricts the fields to be returned
)

Bu sorgu aşağıdaki sonucu döndürür.

[
  {
    "_id": "988d2dd1-2faa-4072-b420-b91b95cbfd60",
    "name": "Lakeshore Retail",
    "sales": {
      "salesByCategory": [
        {
          "categoryName": "Towel Racks",
          "totalSales": 13237
        }
      ]
    }
  }
]

Örnek 4: Diziden son öğeyi getirme

Bu sorgu son belgeyi "sales.salesByCategory" dizisinden alır.

db.stores.find({
    name: "Lakeshore Retail"
}, {
    _id: 1,
    name: 1,
    "sales.salesByCategory": {
        $slice: -1
    }
})

Bu sorgu aşağıdaki sonucu döndürür.

[
  {
    "_id": "988d2dd1-2faa-4072-b420-b91b95cbfd60",
    "name": "Lakeshore Retail",
    "sales": {
      "salesByCategory": [
        {
          "categoryName": "Pillow Cases",
          "totalSales": 38833
        }
      ]
    }
  }
]

Örnek 5: Diziden öğe aralığı alma

Bu sorgu "sales.salesByCategory" dizisinden bir alt küme aralığı alır.

db.stores.find({
    name: "Lakeshore Retail"
}, {
    _id: 1,
    name: 1,
    "sales.salesByCategory": {
        $slice: [3, 2]
    }
})

Bu sorgu aşağıdaki sonucu döndürür.

[
  {
    "_id": "988d2dd1-2faa-4072-b420-b91b95cbfd60",
    "name": "Lakeshore Retail",
    "sales": {
      "salesByCategory": [
        {
          "categoryName": "Toothbrush Holders",
          "totalSales": 47912
        },
        {
          "categoryName": "Hybrid Mattresses",
          "totalSales": 48660
        }
      ]
    }
  }
]