次の方法で共有


$densify

集計パイプラインの $densify ステージは、値のシーケンス内に不足しているデータ ポイントを入力するために使用されます。 指定したフィールド、範囲、ステップに基づいて欠損値を生成することで、より完全なデータセットを作成するのに役立ちます。 これは、正確な分析を確保するためにデータ ポイントのギャップを埋める必要がある時系列データ分析などのシナリオで役立ちます。

構文

{
  $densify: {
    field: <field>,
    range: {
      step: <number>,
      unit: <string>, // Optional, e.g., "hour", "day", "month", etc.
      bounds: [<lowerBound>, <upperBound>] // Optional
    },
    partitionByFields: [<field1>, <field2>, ...] // Optional
  }
}

パラメーター

パラメーター Description
field 密度化が実行されるフィールド。
range.step 欠損値を生成するためのステップ サイズ。
range.unit (省略可能)ステップ サイズの単位 (時間単位など)(例: "hour"、"day")。
range.bounds (省略可能)密度化の範囲 (下限と上限) を指定します。
partitionByFields (省略可能)密度を高めるためにデータをグループ化するために使用されるフィールド。

例示

stores コレクションのこのサンプル ドキュメントについて考えてみましょう。

{
    "_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: 時系列データセットを密度化する

このクエリでは、日付フィールドに不足している日数が入力されます。

db.aggregate([
    {
      $documents: [
        { date: new ISODate("2024-01-01"), value: 10 },
        { date: new ISODate("2024-01-03"), value: 15 }
      ]
    },
    {
      $densify: {
        field: "date",
        range: {
          step: 1,
          unit: "day",
          bounds: "full"
        }
      }
    }
  ]);

このクエリは、次の結果を返します。

[
    {
        "date": "ISODate('2024-01-01T00:00:00.000Z')",
        "value": 10
    },
    {
        "date": "ISODate('2024-01-02T00:00:00.000Z')"
    },
    {
        "date": "ISODate('2024-01-03T00:00:00.000Z')",
        "value": 15
    }
]

例 2: 数値データを密度化する

このクエリでは、 sales.fullSales フィールドに不足している数値が入力されます。

db.aggregate([
    {
      $documents: [
        { level: 1, score: 10 },
        { level: 3, score: 30 }
      ]
    },
    {
      $densify: {
        field: "level",
        range: {
          step: 1,
          bounds: [1, 5] 
        }
      }
    }
  ]);

このクエリは、次の結果を返します。

[
    {
        "level": 1,
        "score": 10
    },
    {
        "level": 2
    },
    {
        "level": 3,
        "score": 30
    },
    {
        "level": 4
    }
]

制限事項

次の表は、集計パイプラインの$densify ステージに関連付けられている主な制限事項と動作をまとめたものです。

カテゴリ 条件/動作
フィールドの制限 - ドキュメントに 日付 値があり、 unit が指定されていない場合にエラーが 発生します
- ドキュメントに 数値 があり、 unit が指定されている場合にエラーが 発生します
- フィールド名$で始まります$projectを使用して名前を変更します。
partitionByFields - すべてのフィールドが 文字列以外 の値に評価されます。
- フィールド名$で始まります
range.bounds - 下限は 、既存のドキュメントに関係なく、開始値を定義します。
- 下限は包括的です
- 上限は排他的です
- $densify では、範囲外のドキュメントは 除外されません