$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$setWindowFieldsstage 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$setWindowFieldsstage 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按timeStampin ascending order (1), so the earliest odometer reading is first.timeStamp按升序(1)对每个分区中的文档进行排序,因此最早的里程表读数是第一位的。outputsets themilesderivative value in a new field calledtruckAverageSpeedusing$derivativethat 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$derivativeunit is set to"hour"for thetimeStampfield, which is used in the denominator for the derivative calculation.timeStamp字段的导数unit设置为"hour",该字段用于导数计算的分母。The window contains the range between a lower limit of该窗口包含介于-30seconds (the previous 30 seconds from the current document in the output) and0seconds (matches the current document'stimeStampvalue in the output). This means$derivativereturns the average speed for each truck in miles per hour in the 30 second window.-30秒(输出中当前文档的前30秒)和0秒(与输出中当前文件的timeStamp值匹配)之间的下限range。这意味着$derivative将返回30秒窗口内每辆卡车的平均速度(以英里/小时为单位)。
The$matchstage uses the greater than operator$gtto 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 }