Definição
Novidade na versão 5.0.
Retorna a taxa média de alteração dentro da janela especificada, que é calculada usando:
Primeiro e último documentos na
$setWindowFieldsjanela de estágio .
$derivative is only available in the $setWindowFields stage. You must specify a window in the $setWindowFields stage when using $derivative.
$derivative sintaxe:
{ $derivative: { input: <expression>, unit: <time unit> } }
$derivative pega um documento com estes campos:
Campo | Descrição |
|---|---|
Especifica a expressão para avaliar. A expressão deve avaliar para um número. | |
Comportamento
You must specify a window in the $setWindowFields stage when using $derivative.
Exemplo
Crie uma coleção deliveryFleet que contenha leituras de odômetro para caminhão de entrega registrados em 30 segundos de intervalo:
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 } } } ] )
No exemplo:
O estágio
$setWindowFieldsobtém a velocidade média em milhas por hora para cada caminhão:partitionBy: "$truckID"particiona os documentos na coleção portruckID.sortBy: { timeStamp: 1 }classifica os documentos em cada partição portimeStampem ordem crescente (1), de modo que a leitura mais antiga do odômetro seja a primeira.outputsets themilesderivative value in a new field calledtruckAverageSpeedusing$derivativethat is run in a range window.The input expression is set to
"$miles", which is used in the numerator for the derivative calculation.The
$derivativeunit is set to"hour"for thetimeStampfield, which is used in the denominator for the derivative calculation.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.
No exemplo de saída a seguir, a velocidade do caminhão 1 é mostrada no campo truckAverageSpeed . A velocidade do caminhão 2 não é exibida porque o caminhão 2 não excedeu 50 milhas por hora.
{ "_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 }