定義
バージョン 5.0 の新機能。
曲線下面積の近似値を返します。この値は、隣接するドキュメントの各セットがスクリプトを形成する スクリプトのルールを使用して計算されます
統合間隔の ステージの
$setWindowFieldssortBy フィールド値。input field expression result values in
$integralfor the y axis values.
$integral is only available in the $setWindowFields stage.
$integral 構文:
{ $integral: { input: <expression>, unit: <time unit> } }
$integral 次のフィールドを持つドキュメントを取ります。
動作
ウィンドウを省略すると、上限と下限がないデフォルトのウィンドウが使用されます。
例
メートル デバイスによって 30 秒間隔で測定された電気使用量をキロバイト単位で含むpowerConsumptionコレクションを作成します。
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"はコレクション内のドキュメントをpowerMeterIDで分割します。sortBy: { timeStamp: 1 }は、各パーティション内のドキュメントをtimeStampで昇順(1)に並べ替えるので、最も古いtimeStampが最初になります。outputsets thekilowattsintegral value in a new field calledpowerMeterKilowattHoursusing$integralthat is run in a range window.The input expression is set to
"$kilowatts", which is used for the y axis values in the integral calculation.The window contains documents between an
unboundedlower limit and thecurrentdocument in the output. This means$integralreturns 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.
この出力例では、1 メートルと 2 メートルで測定された消費量がpowerMeterKilowattHoursフィールドに表示されています。
{ "_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 }
Tip
IoT 電力消費に関する追加の例については、電子書籍『 MongoDB の実用的な集計 』を参照してください。