AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

$integral(式演算子)

バージョン 5.0 の新機能。

$integral

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

$integral is only available in the $setWindowFields stage.

$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 }
] )

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

  • output sets the kilowatts integral value in a new field called powerMeterKilowattHours using $integral that 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 $integral unit is set to "hour" for the timeStamp field, which means $integral returns the kilowatt-hours energy consumption.

    • The window contains documents between an unbounded lower limit and the current 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.

この出力例では、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 の実用的な集計 』を参照してください。

このページを評価

項目一覧