On this page本页内容
New in version 5.0.在版本5.0中新增。
Returns the average rate of change within the specified window, which is calculated using the:返回指定窗口内的平均变化率,使用以下公式计算:
$setWindowFields stage window.$setWindowFields阶段窗口中的第一个和最后一个文档。$derivative is only available in the 仅在$setWindowFields stage. $setWindowFields阶段可用。You must specify a window in the 使用$setWindowFields stage when using $derivative.$derivative时,必须在$setWindowFields阶段中指定一个窗口。
$derivative syntax:语法:
{
$derivative: {
input: <expression>,
unit: <time unit>
}
}
$derivative takes a document with these fields:获取包含以下字段的文档:
input | |
unit |
|
You must specify a window in the 使用$setWindowFields stage when using $derivative.$derivative时,必须在$setWindowFields阶段中指定一个窗口。
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" truckID.truckID对集合中的文档进行分区。sortBy: { timeStamp: 1 } timeStamp in ascending order (1), so the earliest odometer reading is first.timeStamp按升序(1)对每个分区中的文档进行排序,因此最早的里程表读数为第一。output sets the 使用在范围窗口中运行的miles derivative value in a new field called truckAverageSpeed using $derivative that is run in a range window.$derivative在名为truckAverageSpeed的新字段中设置miles导数值。
"$miles", which is used in the numerator for the derivative calculation."$miles",用于导数计算的分子。$derivative unit is set to "hour" for the timeStamp field, which is used in the denominator for the derivative calculation.timeStamp字段的$derivativeunit设置为“小时”,用于导数计算的分母。-30 seconds (the previous 30 seconds from the current document in the output) and 0 seconds (matches the current document's timeStamp value in the output). -30秒(输出中当前文档的前30秒)和0秒(与输出中的当前文档的timeStamp值匹配)之间的下限。$derivative returns the average speed for each truck in miles per hour in the 30 second window.$derivative返回30秒窗口中每辆卡车的平均速度(以英里/小时为单位)。$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 }