$integral (aggregation)
定義
バージョン 5.0 で追加
Returns the approximation of the area under a curve, which is calculated using the trapezoidal rule where each set of adjacent documents form a trapezoid using the:
sortBy field values in the
$setWindowFields
stage for the integration intervals.入力 フィールド 式 result values in
$integral
for the y axis values.
$integral
は$setWindowFields
ステージでのみ使用可能です。
$integral
構文:
{ $integral: { input: <expression>, unit: <time unit> } }
$integral
は次のフィールドを持つドキュメントを取得します。
フィールド | 説明 |
---|---|
Specifies the 式 to evaluate. You must provide an expression that returns a number. | |
動作
If you omit a ウィンドウ, a default window with unbounded upper and lower limits is used.
例
Create a powerConsumption
collection that contains electrical power
usage in kilowatts measured by meter devices at 30 second intervals:
db.powerConsumption.insertMany( [ { powerMeterID: "1", timeStamp: new Date( "2020-05-18T14:10:30Z" ), kilowatts: 2.95 }, { powerMeterID: "1", timeStamp: new Date( "2020-05-18T14:11:00Z" ), kilowatts: 2.7 }, { powerMeterID: "1", timeStamp: new Date( "2020-05-18T14:11:30Z" ), kilowatts: 2.6 }, { powerMeterID: "1", timeStamp: new Date( "2020-05-18T14:12:00Z" ), kilowatts: 2.98 }, { powerMeterID: "2", timeStamp: new Date( "2020-05-18T14:10:30Z" ), kilowatts: 2.5 }, { powerMeterID: "2", timeStamp: new Date( "2020-05-18T14:11:00Z" ), kilowatts: 2.25 }, { powerMeterID: "2", timeStamp: new Date( "2020-05-18T14:11:30Z" ), kilowatts: 2.75 }, { powerMeterID: "2", timeStamp: new Date( "2020-05-18T14:12:00Z" ), kilowatts: 2.82 } ] )
This example uses $integral
in the $setWindowFields
stage to output the energy consumption in kilowatt-hours measured
by each meter device:
db.powerConsumption.aggregate( [ { $setWindowFields: { partitionBy: "$powerMeterID", sortBy: { timeStamp: 1 }, output: { powerMeterKilowattHours: { $integral: { input: "$kilowatts", unit: "hour" }, window: { range: [ "unbounded", "current" ], unit: "hour" } } } } } ] )
この例では、次のことが行われます。
partitionBy: "$powerMeterID"
partitions the documents in the collection bypowerMeterID
.sortBy: { timeStamp: 1 }
は、各パーティション内のドキュメントをtimeStamp
を基準に昇順(1
)にソートするため、最も古い
timeStamp
が最初になります。output
sets thekilowatts
integral value in a new field calledpowerMeterKilowattHours
using$integral
that is run in a 範囲 window.The 入力 expression is set to
"$kilowatts"
, which is used for the y axis values in the integral calculation.The
$integral
単位 is set to"hour"
for thetimeStamp
field, which means$integral
returns the kilowatt-hours energy consumption.The ウィンドウ contains documents between an
unbounded
lower limit and thecurrent
document in the output. This means$integral
returns the total kilowatt-hours energy consumption for the documents from the beginning of the partition, which is the first data point in the partition for each power meter, to the timestamp of the current document in the output.
In this example output, the energy consumption measured by meters 1 and
2 are shown in the powerMeterKilowattHours
field:
{ "_id" : ObjectId("60cbdc3f833dfeadc8e62863"), "powerMeterID" : "1", "timeStamp" : ISODate("2020-05-18T14:10:30Z"), "kilowatts" : 2.95, "powerMeterKilowattHours" : 0 } { "_id" : ObjectId("60cbdc3f833dfeadc8e62864"), "powerMeterID" : "1", "timeStamp" : ISODate("2020-05-18T14:11:00Z"), "kilowatts" : 2.7, "powerMeterKilowattHours" : 0.023541666666666666 } { "_id" : ObjectId("60cbdc3f833dfeadc8e62865"), "powerMeterID" : "1", "timeStamp" : ISODate("2020-05-18T14:11:30Z"), "kilowatts" : 2.6, "powerMeterKilowattHours" : 0.045625 } { "_id" : ObjectId("60cbdc3f833dfeadc8e62866"), "powerMeterID" : "1", "timeStamp" : ISODate("2020-05-18T14:12:00Z"), "kilowatts" : 2.98, "powerMeterKilowattHours" : 0.068875 } { "_id" : ObjectId("60cbdc3f833dfeadc8e62867"), "powerMeterID" : "2", "timeStamp" : ISODate("2020-05-18T14:10:30Z"), "kilowatts" : 2.5, "powerMeterKilowattHours" : 0 } { "_id" : ObjectId("60cbdc3f833dfeadc8e62868"), "powerMeterID" : "2", "timeStamp" : ISODate("2020-05-18T14:11:00Z"), "kilowatts" : 2.25, "powerMeterKilowattHours" : 0.019791666666666666 } { "_id" : ObjectId("60cbdc3f833dfeadc8e62869"), "powerMeterID" : "2", "timeStamp" : ISODate("2020-05-18T14:11:30Z"), "kilowatts" : 2.75, "powerMeterKilowattHours" : 0.040625 } { "_id" : ObjectId("60cbdc3f833dfeadc8e6286a"), "powerMeterID" : "2", "timeStamp" : ISODate("2020-05-18T14:12:00Z"), "kilowatts" : 2.82, "powerMeterKilowattHours" : 0.06383333333333334 }
以下も参照してください。
消費に関する追加の例については、次を参照してください:IoT MongoDB電子書籍。