$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 |
執行 Densification 的欄位。 |
range.step |
產生遺漏值的步驟大小。 |
range.unit |
(選擇性)步驟大小的單位,例如時間單位(例如「小時」、「日」)。 |
range.bounds |
(選擇性)指定 densification 的範圍(下限和上限)。 |
partitionByFields |
(選擇性)用來將數據分組以進行 Densification 的欄位。 |
範例
請參考商店集合中的此範例檔。
{
"_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:Densify 時間序列數據集
此查詢會填入日期欄位中遺漏的天數。
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:Densify 數值數據
此查詢會填入欄位中 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
不會篩選出界限以外的檔。 |