$derivative (aggregation)
定義
バージョン 5.0 で追加
Returns the average rate of change within the specified ウィンドウ, which is calculated using the:
First and last documents in the
$setWindowFields
stage ウィンドウ.Numerator, which is set to the result of subtracting the numeric 式 value for the first document from the 式 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.
$derivative
is only available in the
$setWindowFields
stage. You must specify a ウィンドウ in the $setWindowFields
stage when
using $derivative
.
$derivative
構文:
{ $derivative: { input: <expression>, unit: <time unit> } }
$derivative
は次のフィールドを持つドキュメントを取得します。
動作
You must specify a ウィンドウ in the
$setWindowFields
stage when using $derivative
.
例
Create a deliveryFleet
collection that contains odometer
readings for delivery trucks recorded at 30 second intervals:
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:
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 } } } ] )
この例では、次のことが行われます。
The
$setWindowFields
stage obtains the average speed in miles per hour for each truck:partitionBy: "$truckID"
はコレクション内のドキュメントをtruckID
でパーティショニングします。
sortBy: { timeStamp: 1 }
sorts the documents in each partition bytimeStamp
in ascending order (1
), so the earliest odometer reading is first.output
sets themiles
derivative value in a new field calledtruckAverageSpeed
using$derivative
that is run in a 範囲 window.The 入力 expression is set to
"$miles"
, which is used in the numerator for the derivative calculation.The
$derivative
単位 is set to"hour"
for thetimeStamp
field, which is used in the denominator for the derivative calculation.The ウィンドウ contains the 範囲 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.
The
$match
stage uses the greater than operator$gt
to filter the results to trucks whose speed exceeded 50 miles per hour.
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.
{ "_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 }