Docs Menu
Docs Home
/
MongoDB マニュアル
/ / /

$integral(集計)

項目一覧

  • 定義
  • 動作

バージョン 5.0 で追加

$integral

曲線下面積の近似値を返します。この値は、隣接するドキュメントの各セットがスクリプトを形成する スクリプトのルールを使用して計算されます

  • 統合間隔の ステージの $setWindowFieldssortBy フィールド値。

  • 入力フィールドの結果の値は y 軸の値に対して$integralになる。

$integral$setWindowFieldsステージでのみ使用可能です。

$integral 構文:

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

$integral 次のフィールドを持つドキュメントを取ります。

フィールド
説明

評価するを指定します。 数値を返す 式 を指定する必要があります。

時間単位を指定するstring 。 次のいずれかの文字列を使用します。

  • "week"

  • "day"

  • "hour"

  • "minute"

  • "second"

  • "millisecond"

sortByフィールドが日付でない場合は、 unitを省略する必要があります。 unitを指定する場合は、 sortByフィールドに日付を指定する必要があります。

ウィンドウを省略すると、上限と下限がないデフォルトのウィンドウが使用されます。

メートル デバイスによって 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 }
] )

この例では、 ステージの$integral $setWindowFieldsを使用して、各メートルデバイスによって測定されたエします。

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 が最初になります。

  • output kilowattsは、powerMeterKilowattHours 範囲 ウィンドウで実行される$integral を使用して、 という新しいフィールドに の整数値を設定します。

    • 入力式は"$kilowatts"に設定されます。これは整数計算の y 軸の値に使用されます。

    • timeStampフィールドの$integral単位"hour"に設定されています。つまり、 $integralは キロ検討して 1 時間あたりの消費量を返します。

    • ウィンドウには、 unbounded下限とcurrentドキュメントの間にあるドキュメントが出力されます。 つまり、 $integralは、出力内の現在のドキュメントのタイムスタンプまでのドキュメントの合計 1 キロシークレット 消費量を返します。

この出力例では、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 集計 を参照してください 電子書籍。

戻る

$indexOfCP

項目一覧