“文档” 菜单
文档首页
/
MongoDB Manual
/ / /

$locf(聚合)

在此页面上

  • 定义
  • 语法
  • 行为
  • 举例
$locf

5.2 版本中的新增功能

最后一次观察结果被延续了下来。将窗口null和缺失字段的值设置为该字段的最后一个非空值。

$locf 只能在 $setWindowFields 阶段使用。

$locf 表达式的语法如下:

{ $locf: <expression> }

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

如果填充的字段同时包含null和非空值,则$locf会根据$setWindowFields中指定的排序顺序将null和缺失值设置为字段的最后一个已知非空值。

null 而排序顺序中出现在非空值之前的缺失字段值仍为 null

如果填充的字段仅包含null分区中的缺失值,则$locf会将该分区的字段值设置为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 字段。

以下示例使用$locf操作符将缺失字段设置为上次观察到的非null值中的值:

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

在示例中:

  • sortBy: { time: 1 }time 以升序 (1) 对每个分区中的文档进行排序,因此最早的 time 位于最前面。

  • 对于缺少price字段的文档, $locf操作符会将price设置为序列中最后观察到的值。

示例输出:

[
{
_id: ObjectId("62169b65394d47411658b5f5"),
time: ISODate("2021-03-08T09:00:00.000Z"),
price: 500
},
{
_id: ObjectId("62169b65394d47411658b5f6"),
time: ISODate("2021-03-08T10:00:00.000Z"),
price: 500
},
{
_id: ObjectId("62169b65394d47411658b5f7"),
time: ISODate("2021-03-08T11:00:00.000Z"),
price: 515
},
{
_id: ObjectId("62169b65394d47411658b5f8"),
time: ISODate("2021-03-08T12:00:00.000Z"),
price: 515
},
{
_id: ObjectId("62169b65394d47411658b5f9"),
time: ISODate("2021-03-08T13:00:00.000Z"),
price: 515
},
{
_id: ObjectId("62169b65394d47411658b5fa"),
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 填充缺失的priceprice 基于序列中周围的 值。

    • 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
}
]
← $ln(聚合)