$sampleRate

運算子 $sampleRate 會根據指定的機率隨機取樣集合中的檔。 此運算元適用於統計分析、使用子集數據進行測試,以及在您需要代表性範例的大型數據集時進行效能優化。

語法

{
  $match: {
    $sampleRate: <number>
  }
}

參數

參數 Description
number 介於 0 到 1 之間的浮點數,代表檔將包含在範例中的機率。 例如,0.33 表示會取樣大約 33% 的檔。

範例

請參考商店集合中的此範例檔。

{
    "_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:基本隨機抽樣

此查詢會隨機傳回 Stores 集合中所有檔的三分之一。

db.stores.aggregate([{
    $match: {
        $sampleRate: 0.33
    }
}])

範例 2:使用更多篩選器進行取樣

此查詢會先篩選銷售超過 50,000 的存放區,然後隨機取樣 50 個相符檔%。

db.stores.aggregate([
  { $match: { 
    "sales.totalSales": { $gt: 50000 },
    $sampleRate: 0.5 
  }}
])

範例 3:統計分析的抽樣

此查詢會取樣 25 個存放區%,並計算取樣數據的統計量值。

db.stores.aggregate([
  { $match: { $sampleRate: 0.25 } },
  { $group: {
    _id: null,
    averageSales: { $avg: "$sales.totalSales" },
    totalStores: { $sum: 1 },
    maxSales: { $max: "$sales.totalSales" },
    minSales: { $min: "$sales.totalSales" }
  }}
])

使用大型數據集時,$sampleRate運算元對於統計分析和數據探索而言非常重要,因為處理所有檔的成本會很高。 其可有效率地建立效能測試、品質保證驗證和機器學習數據集產生的代表性範例。 運算子適用於可接受統計精確度且處理速度優先於精確精確度的近似報告案例。