$derivative (aggregation)
On this page本页内容
Definition定义
New in version 5.0. 5.0版新增。
$derivative
Returns the average rate of change within the specified window, which is calculated using the:返回指定窗口内的平均变化率,该平均变化率使用以下公式计算:
First and last documents in the$setWindowFields
stage window.$setWindowFields
阶段窗口中的第一个和最后一个文档。Numerator, which is set to the result of subtracting the numeric expression value for the first document from the expression value for the last document.Numerator,设置为从最后一个文档的表达式值中减去第一个文档的数值表达式值的结果。Denominator, which is set to the result of subtracting the sortBy field value for the first document from the sortBy field value for the last document.分母,它被设置为从最后一个文档的sortBy
字段值中减去第一个文档的sortBy
字段值的结果。
$derivative
is only available in the 仅在$setWindowFields
stage. $setWindowFields
阶段中可用。You must specify a window in the 使用$setWindowFields
stage when using $derivative
.$derivative
时,必须在$setWindowFields
stage when using $derivative
阶段中指定一个窗口。
$derivative
syntax:语法:
{
$derivative: {
input: <expression>,
unit: <time unit>
}
}
$derivative
takes a document with these fields:获取包含以下字段的文档:
input | |
unit | string that specifies the time unit. Use one of these strings: string 。使用以下字符串之一:
unit . If you specify a unit , you must specify a date in the sortBy field. sortBy 字段不是日期,则必须省略一个unit 。如果指定unit ,则必须在sortBy 字段中指定日期。 |
Behavior行为
You must specify a window in the 使用$setWindowFields
stage when using $derivative
.$derivative
时,必须在$setWindowFields
阶段中指定一个窗口。
Example实例
Create a 创建一个deliveryFleet
collection that contains odometer readings for delivery trucks recorded at 30 second intervals:deliveryFleet
集合,其中包含每隔30秒记录的送货卡车的里程表读数:
db.deliveryFleet.insertMany( [
{ truckID: "1", timeStamp: new Date( "2020-05-18T14:10:30Z" ), miles: 1295.1 },
{ truckID: "1", timeStamp: new Date( "2020-05-18T14:11:00Z" ), miles: 1295.63 },
{ truckID: "1", timeStamp: new Date( "2020-05-18T14:11:30Z" ), miles: 1296.25 },
{ truckID: "1", timeStamp: new Date( "2020-05-18T14:12:00Z" ), miles: 1296.76 },
{ truckID: "2", timeStamp: new Date( "2020-05-18T14:10:30Z" ), miles: 10234.1 },
{ truckID: "2", timeStamp: new Date( "2020-05-18T14:11:00Z" ), miles: 10234.33 },
{ truckID: "2", timeStamp: new Date( "2020-05-18T14:11:30Z" ), miles: 10234.73 },
{ truckID: "2", timeStamp: new Date( "2020-05-18T14:12:00Z" ), miles: 10235.13 }
] )
This example uses 本例在$derivative
in the $setWindowFields
stage to obtain the average speed in miles per hour for each truck, and the $match
stage to filter the results to trucks whose speed exceeded 50 miles per hour:$setWindowFields
阶段使用$derivative
来获得每辆卡车的平均速度(以英里/小时为单位),在$match
阶段将结果筛选到速度超过50英里/小时的卡车:
db.deliveryFleet.aggregate( [
{
$setWindowFields: {
partitionBy: "$truckID",
sortBy: { timeStamp: 1 },
output: {
truckAverageSpeed: {
$derivative: {
input: "$miles",
unit: "hour"
},
window: {
range: [ -30, 0 ],
unit: "second"
}
}
}
}
},
{
$match: {
truckAverageSpeed: {
$gt: 50
}
}
}
] )
In the example:在示例中:
The$setWindowFields
stage obtains the average speed in miles per hour for each truck:$setWindowFields
阶段获得每辆卡车的平均速度(以英里/小时为单位):partitionBy: "$truckID"
partitions the documents in the collection by按truckID
.truckID
对集合中的文档进行分区。sortBy: { timeStamp: 1 }
sorts the documents in each partition by按timeStamp
in ascending order (1
), so the earliest odometer reading is first.timeStamp
按升序(1
)对每个分区中的文档进行排序,因此最早的里程表读数是第一位的。output
sets themiles
derivative value in a new field calledtruckAverageSpeed
using$derivative
that is run in a range window.output
使用在range
窗口中运行的$derivative
在一个名为truckAverageSpeed
的新字段中设置miles
导数值。The input expression is set to"$miles"
, which is used in the numerator for the derivative calculation.input
表达式设置为"$miles"
,用于导数计算的分子中。The$derivative
unit is set to"hour"
for thetimeStamp
field, which is used in the denominator for the derivative calculation.timeStamp
字段的导数unit
设置为"hour"
,该字段用于导数计算的分母。The window contains the range between a lower limit of该窗口包含介于-30
seconds (the previous 30 seconds from the current document in the output) and0
seconds (matches the current document'stimeStamp
value in the output). This means$derivative
returns the average speed for each truck in miles per hour in the 30 second window.-30
秒(输出中当前文档的前30秒)和0
秒(与输出中当前文件的timeStamp
值匹配)之间的下限range
。这意味着$derivative
将返回30秒窗口内每辆卡车的平均速度(以英里/小时为单位)。
The$match
stage uses the greater than operator$gt
to filter the results to trucks whose speed exceeded 50 miles per hour.$match
阶段使用大于运算符$gt
将结果筛选到速度超过每小时50英里的卡车。
In the following example output, the speed for truck 1 is shown in the 在以下输出示例中,卡车1的速度显示在truckAverageSpeed
field. truckAverageSpeed
字段中。The speed for truck 2 is not shown because truck 2 did not exceed 50 miles per hour.没有显示卡车2的速度,因为卡车2没有超过每小时50英里。
{ "_id" : ObjectId("60cb8a7e833dfeadc8e6285c"), "truckID" : "1",
"timeStamp" : ISODate("2020-05-18T14:11:00Z"), "miles" : 1295.63,
"truckAverageSpeed" : 63.60000000002401 }
{ "_id" : ObjectId("60cb8a7e833dfeadc8e6285d"), "truckID" : "1",
"timeStamp" : ISODate("2020-05-18T14:11:30Z"), "miles" : 1296.25,
"truckAverageSpeed" : 74.3999999999869 }
{ "_id" : ObjectId("60cb8a7e833dfeadc8e6285e"), "truckID" : "1",
"timeStamp" : ISODate("2020-05-18T14:12:00Z"), "miles" : 1296.76,
"truckAverageSpeed" : 61.199999999998916 }