Database Manual / Reference / Query Language / Expressions

$derivative(求导) (Window Function)(窗口函数)

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.分子,设置为从最后一个文档的表达式值中减去第一个文档的数值表达式值的结果。
  • 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阶段指定一个窗口

$derivative syntax:语法:

{
$derivative: {
input: <expression>,
unit: <time unit>
}
}

$derivative takes a document with these fields:获取包含以下字段的文档:

Field字段Description描述
input

Specifies the expression to evaluate. The expression must evaluate to a number.指定要计算的表达式。表达式的计算结果必须为数字。

unit

A 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.使用在范围窗口中运行的$derivative在名为truckAverageSpeed的新字段中设置miles导数值。

      • The input expression is set to "$miles", which is used in the numerator for the derivative calculation.输入表达式设置为"$miles",用于导数计算的分子。
      • The $derivative unit is set to "hour" for the timeStamp field, which is used in the denominator for the derivative calculation.timeStamp字段的$derivative 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). 窗口包含-30秒(输出中当前文档的前30秒)和0秒(与输出中的当前文档的timeStamp值匹配)之间的下限范围This means $derivative returns the average speed for each truck in miles per hour in the 30 second window.这意味着$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. The speed for truck 2 is not shown because truck 2 did not exceed 50 miles per hour.在以下示例输出中,卡车1的速度显示在truckAverageSpeed字段中。卡车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 }