Docs 菜单

Docs 主页开发应用程序MongoDB Manual

$linearFill(聚合)

在此页面上

  • 定义
  • 语法
  • 行为
  • 举例
  • 限制
$linearFill

5.3 版本中的新增功能

使用 线性插值 填充null 窗口 中的 和缺失字段 基于周围字段值。

$linearFill仅在$setWindowFields阶段可用。

$linearFill表达式的语法如下:

{ $linearFill: <expression> }

有关表达式的更多信息,请参阅表达式

使用 线性插值$linearFill 填充null 和缺失字段 基于周围的非 字段值。周围的字段值由null$setWindowFields 中指定的排序顺序确定。

  • $linearFill按比例填充null和缺失值,这些值跨越周围的非null值之间的值范围。为了确定缺失字段的值, $linearFill使用:

    • 周围非 null 值之差。

    • 周围值之间要填充的 null 字段的数量。

  • $linearFill可以填充多个连续的null值,前提是根据 $setWindowFields 中指定的排序顺序,这些值的前后都是非null

    例子

    如果集合中包含这些文档:

    { index: 0, value: 0 },
    { index: 1, value: null },
    { index: 2, value: null },
    { index: 3, value: null },
    { index: 4, value: 10 }

    使用$linearFill填充null值后,文档变为:

    { index: 0, value: 0 },
    { index: 1, value: 2.5 },
    { index: 2, value: 5 },
    { index: 3, value: 7.5 },
    { index: 4, value: 10 }

    有关完整示例,请参阅示例。

  • null 前后没有非 null 值的值仍为 null

使用 线性插值 填充缺失字段值 ,您可以使用:

本页上的示例使用stock集合,其中包含每小时跟踪单个公司的股票价格:

db.stock.insertMany( [
{
time: ISODate("2021-03-08T09:00:00.000Z"),
price: 500
},
{
time: ISODate("2021-03-08T10:00:00.000Z"),
},
{
time: ISODate("2021-03-08T11:00:00.000Z"),
price: 515
},
{
time: ISODate("2021-03-08T12:00:00.000Z")
},
{
time: ISODate("2021-03-08T13:00:00.000Z")
},
{
time: ISODate("2021-03-08T14:00:00.000Z"),
price: 485
}
] )

集合中的某些文档缺少 price 字段。

使用 线性插值 填充缺失的price 值 ,在 阶段内使用$linearFill $setWindowFields

db.stock.aggregate( [
{
$setWindowFields:
{
sortBy: { time: 1 },
output:
{
price: { $linearFill: "$price" }
}
}
}
] )

在示例中:

  • sortBy: { time: 1 }time 字段以升序排列文档,从最早到最晚。

  • 输出指定:

    • price 作为要填写缺失值的字段。

    • { $linearFill: "$price" } 作为缺失字段的值。$linearFill price使用 线性插值 填充缺失的 值price 基于序列中周围的 值。

示例输出:

[
{
_id: ObjectId("620ad555394d47411658b5ef"),
time: ISODate("2021-03-08T09:00:00.000Z"),
price: 500
},
{
_id: ObjectId("620ad555394d47411658b5f0"),
time: ISODate("2021-03-08T10:00:00.000Z"),
price: 507.5
},
{
_id: ObjectId("620ad555394d47411658b5f1"),
time: ISODate("2021-03-08T11:00:00.000Z"),
price: 515
},
{
_id: ObjectId("620ad555394d47411658b5f2"),
time: ISODate("2021-03-08T12:00:00.000Z"),
price: 505
},
{
_id: ObjectId("620ad555394d47411658b5f3"),
time: ISODate("2021-03-08T13:00:00.000Z"),
price: 495
},
{
_id: ObjectId("620ad555394d47411658b5f4"),
time: ISODate("2021-03-08T14:00:00.000Z"),
price: 485
}
]

当您使用$setWindowFields阶段填充缺失值时,您可以为与填充字段不同的字段设置值。因此,您可以在单个$setWindowFields阶段使用多种填充方法,并在不同字段中输出结果。

以下管道使用price 线性插值 填充缺失的 字段 以及最后观察结转方法:

db.stock.aggregate( [
{
$setWindowFields:
{
sortBy: { time: 1 },
output:
{
linearFillPrice: { $linearFill: "$price" },
locfPrice: { $locf: "$price" }
}
}
}
] )

在示例中:

  • sortBy: { time: 1 }time 字段以升序排列文档,从最早到最晚。

  • 输出指定:

    • linearFillPrice 作为要填充的目标字段。

      • { $linearFill: "$price" }linearFillPrice 字段的值。$linearFill price使用 线性插值 填充缺失的 值price 基于序列中周围的 值。

    • locfPrice 作为要填充的目标字段。

      • { $locf: "$price" }locfPrice字段的值。 locf代表最后的观察结果。 $locf使用序列中上一个文档中的值填充缺失的price值。

示例输出:

[
{
_id: ObjectId("620ad555394d47411658b5ef"),
time: ISODate("2021-03-08T09:00:00.000Z"),
price: 500,
linearFillPrice: 500,
locfPrice: 500
},
{
_id: ObjectId("620ad555394d47411658b5f0"),
time: ISODate("2021-03-08T10:00:00.000Z"),
linearFillPrice: 507.5,
locfPrice: 500
},
{
_id: ObjectId("620ad555394d47411658b5f1"),
time: ISODate("2021-03-08T11:00:00.000Z"),
price: 515,
linearFillPrice: 515,
locfPrice: 515
},
{
_id: ObjectId("620ad555394d47411658b5f2"),
time: ISODate("2021-03-08T12:00:00.000Z"),
linearFillPrice: 505,
locfPrice: 515
},
{
_id: ObjectId("620ad555394d47411658b5f3"),
time: ISODate("2021-03-08T13:00:00.000Z"),
linearFillPrice: 495,
locfPrice: 515
},
{
_id: ObjectId("620ad555394d47411658b5f4"),
time: ISODate("2021-03-08T14:00:00.000Z"),
price: 485,
linearFillPrice: 485,
locfPrice: 485
}
]
← $let(聚合)