階段 $fill 可用來填入彙總管線內檔中的遺漏或 Null 值。 它提供各種方法來填入遺漏的資料,包括使用靜態值、線性插補或上一個/下一個文件中的值。
語法
{
$fill: {
sortBy: <sort specification>,
partitionBy: <partition fields>,
partitionByFields: <array of partition field names>,
output: {
<field1>: { value: <expression> },
<field2>: { method: <string> }
}
}
}
參數
| 參數 | Description |
|---|---|
sortBy |
在套用相依於文件順序的填滿方法時,指定文件的排序次序。 |
partitionBy |
選擇性。 將文件分組為多個分割區。 填滿作業會分別在每個分割區內套用。 |
partitionByFields |
選擇性。 使用欄位陣列名稱的 partitionBy 替代語法。 |
output |
指定要填滿的欄位,以及用來填入遺漏資料的方法或值。 |
填滿方法
| 方法 | Description |
|---|---|
value |
填滿指定的靜態值或表達式結果。 |
linear |
使用已知值之間的線性插補填滿 (僅限數值欄位)。 |
locf |
最後觀察值前推 (使用最後一個已知值)。 |
linear |
周圍值之間的線性插補。 |
範例
請參考商店集合中的此範例檔。
{
"_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f",
"name": "First Up Consultants | Bed and Bath Center - South Amir",
"location": {
"lat": 60.7954,
"lon": -142.0012
},
"staff": {
"totalStaff": {
"fullTime": 18,
"partTime": 17
}
},
"sales": {
"totalSales": 37701,
"salesByCategory": [
{
"categoryName": "Mattress Toppers",
"totalSales": 37701
}
]
},
"promotionEvents": [
{
"eventName": "Price Drop Palooza",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 9,
"Day": 21
},
"endDate": {
"Year": 2024,
"Month": 9,
"Day": 30
}
},
"discounts": [
{
"categoryName": "Bath Accessories",
"discountPercentage": 18
},
{
"categoryName": "Pillow Top Mattresses",
"discountPercentage": 17
}
]
}
]
}
範例 1:以靜態值填滿遺漏值
此查詢會以預設值 0 填入陣列中的totalSales遺salesByCategory漏值。
db.stores.aggregate([{
$match: {
company: {
$in: ["First Up Consultants"]
}
}
}, {
$unwind: "$sales.salesByCategory"
}, {
$fill: {
output: {
"sales.salesByCategory.totalSales": {
value: 0
}
}
}
}, {
$group: {
_id: "$_id",
name: {
$first: "$name"
},
salesByCategory: {
$push: "$sales.salesByCategory"
}
}
}])
此查詢傳回的前兩個結果是:
[
{
"_id": "affdc09c-7356-4fff-a857-e8301f57159c",
"name": "First Up Consultants | Sports Gear Pantry - Wildermanhaven",
"salesByCategory": [
{
"categoryName": "Baseball Gear",
"totalSales": 33878
},
{
"categoryName": "Volleyball Gear",
"totalSales": 34031
}
]
},
{
"_id": "1cf667b4-d8ce-4f1a-bad1-a1f0bbce26c2",
"name": "First Up Consultants | Picture Frame Variety - New Abrahamborough",
"salesByCategory": [
{
"categoryName": "Picture Hanging Supplies",
"totalSales": 7229
},
{
"categoryName": "Collage Frames",
"totalSales": 40014
}
]
}
]
範例 2:使用最後觀察值前推填滿遺漏的員工資料
此查詢會使用每個商店群組內的最後一個已知值來填入遺失的兼職員工資料。
db.stores.aggregate([{
$fill: {
sortBy: {
"_id": 1
},
output: {
"staff.totalStaff.partTime": {
method: "locf"
}
}
}
}, {
$project: {
name: 1,
"staff.totalStaff": 1
}
}])
此查詢傳回的前兩個結果是:
[
{
"_id": "00003278-4226-4ca7-871d-e80d8f414431",
"name": "Wide World Importers | Camera Depot - Lake Luramouth",
"staff": {
"totalStaff": {
"fullTime": 20,
"partTime": 6
}
}
},
{
"_id": "00009bd0-c44e-4cc8-ab03-347076d74a1a",
"name": "Wide World Importers | Music Stop - Rebeccaside",
"staff": {
"totalStaff": {
"fullTime": 9,
"partTime": 0
}
}
}
]
範例 3:以平均值填滿遺漏折扣百分比
此查詢會以所有商店的平均折扣百分比填入遺漏的折扣百分比。
db.stores.aggregate([
{ $unwind: "$promotionEvents" },
{ $unwind: "$promotionEvents.discounts" },
{
$fill: {
partitionBy: "$promotionEvents.eventName",
sortBy: { "promotionEvents.discounts.categoryName": 1 },
output: {
"promotionEvents.discounts.discountPercentage": {
value: { $avg: "$promotionEvents.discounts.discountPercentage" }
}
}
}
},
{
$group: {
_id: { storeId: "$_id", eventName: "$promotionEvents.eventName" },
storeName: { $first: "$name" },
eventName: { $first: "$promotionEvents.eventName" },
discounts: { $push: "$promotionEvents.discounts" }
}
}
])
此查詢傳回的前兩個結果是:
[
{
"_id": {
"storeId": "70d4cc90-23b1-46e3-8f59-630648e311a4",
"eventName": "Price Slash Spectacular"
},
"storeName": "Wide World Importers | Music Bazaar - West Johnpaulhaven",
"eventName": "Price Slash Spectacular",
"discounts": [
{
"categoryName": "CDs",
"discountPercentage": 22
},
{
"categoryName": "Vinyl Records",
"discountPercentage": 21
}
]
},
{
"_id": {
"storeId": "24873ac4-b2d1-4216-a425-3375a384b23d",
"eventName": "Massive Deal Mania"
},
"storeName": "Northwind Traders | Furniture Pantry - Farrellchester",
"eventName": "Massive Deal Mania",
"discounts": [
{
"categoryName": "Bookcases",
"discountPercentage": 22
},
{
"categoryName": "Cabinets",
"discountPercentage": 8
}
]
}
]
使用案例
- 資料清理:填滿匯入資料集中的遺漏值
- 時間序列資料:使用插補處理循序資料中的間距
- 預設值:將預設值指派給選擇性欄位
- 資料正規化:確保跨文件的資料結構一致