Docs HomeMongoDB Manual

$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:获取包含以下字段的文档:

Field字段Description描述
inputSpecifies the expression to evaluate. The expression must evaluate to a number. 指定要计算的表达式。表达式的计算结果必须为数字。
unitA string that specifies the time unit. Use one of these strings: 指定时间单位的string。使用以下字符串之一:
  • "week"
  • "day"
  • "hour"
  • "minute"
  • "second"
  • "millisecond"
If the sortBy field is not a date, you must omit a 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 the miles derivative value in a new field called truckAverageSpeed 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 the timeStamp 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) and 0 seconds (matches the current document's timeStamp 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 truckAverageSpeed field. 在以下输出示例中,卡车1的速度显示在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 }