문서 메뉴

문서 홈애플리케이션 개발MongoDB 매뉴얼

적분(집계)

이 페이지의 내용

  • 정의
  • 행동
  • 예제

버전 5.0에 추가.

$integral

각 인접한 문서 세트가 다음을 사용하여 사각형을 형성하는 삼각형 규칙을 사용하여 계산된 곡선 아래 영역의 근사치를 반환합니다.

  • 적분 간격에 $setWindowFields 대한 단계의 sortBy 필드값입니다.

  • y축 값에대한 $integral 의 입력 필드 표현식 결과 값입니다.

$integral$setWindowFields 단계에서만 사용할 수 있습니다.

$integral 구문:

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

$integral 는 다음 필드가 있는 문서를 가져옵니다.

필드
설명
입력

평가할 표현식 을 지정합니다. 숫자를 반환하는 표현식을 제공해야 합니다.

시간 단위를 지정하는 string 입니다. 다음 문자열 중 하나를 사용합니다.

  • "week"

  • "day"

  • "hour"

  • "minute"

  • "second"

  • "millisecond"

sortBy 필드가 날짜가 아닌 경우 unit 을(를) 생략해야 합니다. unit 를 지정하는 경우 sortBy 필드에 날짜를 지정해야 합니다.

window 를 생략하면 상한 및 하한이 제한되지 않는 기본 창이 사용됩니다.

미터 장치로 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" 는 collection의 문서를 powerMeterID파티셔닝합니다.

  • sortBy: { timeStamp: 1 } 각 파티션의 문서를 timeStamp을 기준으로 오름차순(1)으로 정렬하므로, 가장 이른 timeStamp이 첫 번째가 됩니다.

  • output 범위 창에서 $integral 실행되는 kilowattspowerMeterKilowattHours 사용하여 라는 새 필드에 정수 값을 설정합니다.

    • 입력 표현식은 적분 계산에서 y축 값에 사용되는 "$kilowatts" 로 설정됩니다.

    • $integral 단위timeStamp 필드에 대해 "hour" 로 설정되며, 이는 $integral 가 킬로와트시 에너지 소비를 반환한다는 것을 의미합니다.

    • 에는 unbounded 하한과 출력의 current 문서 사이의 문서가 포함됩니다. 즉, $integral 는 각 전력계에 대한 파티션의 첫 번째 데이터 포인트인 파티션 의 시작부터 출력에 있는 현재 문서의 타임스탬프까지 문서에 대한 총 킬로와트시 에너지 소비량을 반환합니다.

이 예제 출력에서는 미터 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 }

다음도 참조하세요.

IOT 전력 소비에 대한 추가 예는 실용적인 MongoDB 애그리게이션 을 참조하세요. 전자책.

← $indexOfCP (애그리게이션)

이 페이지의 내용