$locf (aggregation)
On this page本页内容
Definition定义
$locf
New in version 5.2.5.2版新增。Last observation carried forward. Sets values for最后一次观察继续进行。将窗口中null
and missing fields in a window to the last non-null value for the field.null
和缺失字段的值设置为该字段的最后一个非null
值。$locf
is only available in the$setWindowFields
stage.$locf
仅在$setWindowFields
阶段中可用。
Syntax语法
The $locf
expression has this syntax:$locf
表达式的语法如下:
{ $locf: <expression> }
For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
Behavior行为
If a field being filled contains both 如果要填充的字段同时包含null
and non-null values, $locf
sets the null
and missing values to the field's last known non-null value according to the sort order specified in $setWindowFields
.null
值和非null
值,$locf
会根据$setWindowFields
中指定的排序顺序,将null
值和缺失值设置为字段最后一个已知的非null
值。
排序顺序中出现在非null
and missing field values that appear before non-null values in the sort order remain null
.null
值之前的null
和缺失字段值保持为null
。
If a field being filled contains only 如果要填充的字段在分区中只包含null
or missing values in a partition, $locf
sets the field value to null
for that partition.null
或缺少值,$locf
会将该分区的字段值设置为null
。
Comparison of $fill
and $locf
$fill
和$locf
的比较
$fill
and $locf
To fill missing field values based on the last observed value in a sequence, you can use:要根据序列中最后一个观察到的值填充缺失的字段值,可以使用:
The带有$fill
stage with{ method: "locf" }
.{ method: "locf" }
的$fill
阶段。When you use the使用$fill
stage, the field you specify in the output is the same field used as the source data. See Fill Missing Field Values Based on the Last Observed Value.$fill
阶段时,在输出中指定的字段与源数据使用的字段相同。请参见根据上次观测值填充缺少的字段值。The$locf
operator inside of a$setWindowFields
stage.$setWindowFields
阶段内的$locf
运算符。When you use the使用$locf
operator, you can set values for a different field than the field used as the source data. See Use Multiple Fill Methods in a Single Stage.$locf
运算符时,可以为不同于用作源数据的字段的字段设置值。请参见在单个阶段中使用多种填充方法。
Examples实例
The examples on this page use a 此页面上的示例使用了一个股票集合,其中包含按小时间隔跟踪单个公司股价的信息:stock
collection that contains tracks a single company's stock price at hourly intervals:
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
}
] )
The 集合中的某些文档缺少price
field is missing for some of the documents in the collection.price
字段。
Fill Missing Values with the Last Observed Value用上次观测值填充缺失值
The following example uses the 以下示例使用$locf
operator to set missing fields to the value from the last-observed non-null
value:$locf
运算符将缺少的字段设置为上次观察到的非null
值中的值:
db.stock.aggregate( [
{
$setWindowFields: {
sortBy: { time: 1 },
output: {
price: { $locf: "$price" }
}
}
}
] )
In the example:在示例中:
sortBy: { time: 1 }
sorts the documents in each partition by按time
in ascending order (1
), so the earliesttime
is first.time
升序(1
)对每个分区中的文档进行排序,因此最早的time
是第一个。For documents where the对于缺少price
field is missing, the$locf
operator sets theprice
to the last-observed value in the sequence.price
字段的文档,$locf
运算符将price
设置为序列中最后一个观察到的值。
Example output:示例输出:
[
{
_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
}
]
Use Multiple Fill Methods in a Single Stage在单个阶段中使用多种填充方法
When you use the 当您使用$setWindowFields
stage to fill missing values, you can set values for a different field than the field you fill from. $setWindowFields
阶段来填充缺失的值时,您可以为不同于从中填充的字段的字段设置值。As a result, you can use multiple fill methods in a single 因此,您可以在单个$setWindowFields
stage and output the results in distinct fields.$setWindowFields
阶段中使用多个填充方法,并在不同的字段中输出结果。
The following pipeline populates missing 以下管道使用线性插值price
fields using linear interpolation and the last-observation-carried-forward method:
和最后一次观测结转方法填充缺失的
price
字段:
db.stock.aggregate( [
{
$setWindowFields:
{
sortBy: { time: 1 },
output:
{
linearFillPrice: { $linearFill: "$price" },
locfPrice: { $locf: "$price" }
}
}
}
] )
In the example:在示例中:
sortBy: { time: 1 }
sorts the documents by the按照time
field in ascending order, from earliest to latest.time
字段从最早到最晚的升序对文档进行排序。output
specifies:指定:linearFillPrice
as a target field to be filled.作为要填充的目标字段。{ $linearFill: "$price" }
is the value for the是linearFillPrice
field.$linearFill
fills missingprice
values using linear interpolationbased on the surrounding
price
values in the sequence.linearFillPrice
字段的值。$linearFill
使用基于序列中周围price
值的线性插值来填充缺失的
price
值。
locfPrice
as a target field to be filled.作为要填充的目标字段。{ $locf: "$price" }
is the value for the是locfPrice
field.locfPrice
字段的值。locf
stands for last observation carried forward.代表最后一次观测。$locf
fills missing用序列中上一个文档的值填充缺少的price
values with the value from the previous document in the sequence.price
值。
Example output:输出示例:
[
{
_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
}
]