정의
버전 5.0에 추가.
창이라고 하는 컬렉션의 지정된 문서 범위에서 작업을 수행하고 선택한 창 연산자에 따라 결과를 반환합니다.
예를 들어, $setWindowFields 단계를 사용해 다음을 출력할 수 있습니다.
- 컬렉션에 있는 두 문서 간의 판매량 차이입니다. 
- 판매 순위. 
- 누적 판매 총액입니다. 
- 데이터를 외부 데이터베이스로 내보내지 않고도 복잡한 시계열 정보를 분석합니다. 
구문
$setWindowFields 단계 구문:
{    $setWindowFields: {       partitionBy: <expression>,       sortBy: {          <sort field 1>: <sort order>,          <sort field 2>: <sort order>,          ...,          <sort field n>: <sort order>       },       output: {          <output field 1>: {             <window operator>: <window operator parameters>,             window: {                documents: [ <lower boundary>, <upper boundary> ],                range: [ <lower boundary>, <upper boundary> ],                unit: <time unit>             }          },          <output field 2>: { ... },          ...          <output field n>: { ... }       }    } } 
$setWindowFields 단계는 다음 필드가 포함된 문서를 사용합니다.
| 필드 | 필요성 | 설명 | 
|---|---|---|
| 옵션 | 문서를 그룹 하는 표현식 지정합니다.  | |
| 일부 연산자에 필수(제한 사항 참조) | 파티션에서 문서를 정렬할 필드를 지정합니다.  | |
| 필수 사항 | 단계에서 반환된 출력의 문서에 추가할 필드  필드 내장된 문서 필드와 배열 필드를 지정하는  점이 포함될 수 있습니다. 단계의 내장된 문서 점 표기법의  
 | |
| 옵션 | 창 경계 및 매개 변수를 지정합니다. 창 경계는 포괄적입니다. 기본값은 파티션에 있는 모든 문서를 포함하는 바인딩 되지 않은 창입니다. 문서 또는 범위 창 지정합니다. | |
| 옵션 | 컬렉션에서 읽은 현재 문서의 위치를 기준으로 하한 및 상한 경계가 지정되는 창입니다. 창 경계는 하한 및 상한 문자열 또는 정수를 포함하는 두 요소 배열을 사용하여 지정됩니다. 다음을 사용하세요. 
 문서 창 예제를 참조하세요. | |
| 옵션 | ||
| 옵션 | 시간 범위 창 경계의 단위를 지정합니다. 다음 문자열 중 하나로 설정하다 수 있습니다. 
 생략하면 기본값 숫자 범위 창 경계가 사용됩니다. | 
팁
행동
$setWindowFields 단계는 기존 문서에 새 필드를 추가합니다. 집계 연산에 하나 이상의 $setWindowFields 단계를 포함할 수 있습니다.
MongoDB 5.3 트랜잭션 및 "snapshot" 읽기 고려와 함께 $setWindowFields 단계를 사용할 수 있습니다.
$setWindowFields 단계는 반환된 문서의 순서를 보장하지 않습니다.
창 연산자
이러한 연산자는 $setWindowFields 단계와 함께 사용할 수 있습니다.
- 누산기 연산자: - $addToSet,- $avg,- $bottom,- $bottomN,- $count,- $covariancePop,- $covarianceSamp,- $derivative,- $expMovingAvg,- $firstN,- $integral,- $lastN,- $max,- $maxN,- $median,- $min,- $minN,- $percentile,- $push,- $stdDevSamp,- $stdDevPop,- $sum,- $top,- $topN.
- 간격 채우기 연산자 : - $linearFill,- $locf.
- 순위 연산자: - $denseRank,- $documentNumber및- $rank.
제한 사항
다음은 $setWindowFields 단계에 대한 제한 사항입니다.
- MongoDB 5.3 이전에는 - $setWindowFields단계를 사용할 수 없습니다.- 트랜잭션 내. 
- "snapshot"읽기 고려 문제가 있습니다.
 
- sortBy는 다음에 필요합니다. - $linearFill연산자를 사용하세요.
 
- 이러한 연산자는 암시적 창을 사용하며 다음과 같은 창 옵션을 지정하면 오류를 반환합니다. 
- 범위 창의 경우 지정된 범위의 숫자만 창에 포함됩니다. 누락된 값, 정의되지 않은 값, - null값은 제외됩니다.
- 시간 범위 창: - 창에는 날짜 및 시간 유형만 포함됩니다. 
- 숫자 경계 값은 정수여야 합니다. 예를 들어 2시간은 경계로 사용할 수는 있지만 1.5시간은 사용할 수 없습니다. 
 
- 빈 창 또는 호환되지 않는 값을 가진 창(예시: 문자열에 - $sum사용)의 경우 반환되는 값은 연산자에 따라 달라집니다.
예시
캘리포니아주(CA)와 워싱턴주(WA)의 케이크 판매량이 포함된 cakeSales collection을 생성합니다.
db.cakeSales.insertMany( [    { _id: 0, type: "chocolate", orderDate: new Date("2020-05-18T14:10:30Z"),      state: "CA", price: 13, quantity: 120 },    { _id: 1, type: "chocolate", orderDate: new Date("2021-03-20T11:30:05Z"),      state: "WA", price: 14, quantity: 140 },    { _id: 2, type: "vanilla", orderDate: new Date("2021-01-11T06:31:15Z"),      state: "CA", price: 12, quantity: 145 },    { _id: 3, type: "vanilla", orderDate: new Date("2020-02-08T13:13:23Z"),      state: "WA", price: 13, quantity: 104 },    { _id: 4, type: "strawberry", orderDate: new Date("2019-05-18T16:09:01Z"),      state: "CA", price: 41, quantity: 162 },    { _id: 5, type: "strawberry", orderDate: new Date("2019-01-08T06:12:03Z"),      state: "WA", price: 43, quantity: 134 } ] ) 
다음 예시에서는 cakeSales 컬렉션을 사용합니다.
문서 창 예시
문서 창을 사용하여 각 상태에 대한 누적 수량 가져오기
이 예시에서는 $setWindowFields 문서 창을 사용하여 각 state에 대한 누적 케이크 판매량 quantity를 출력합니다.
db.cakeSales.aggregate( [    {       $setWindowFields: {          partitionBy: "$state",          sortBy: { orderDate: 1 },          output: {             cumulativeQuantityForState: {                $sum: "$quantity",                window: {                   documents: [ "unbounded", "current" ]                }             }          }       }    } ] ) 
예시:
- partitionBy: "$state"는 컬렉션의 문서를- state로 파티셔닝합니다.- CA및- WA에 대한 파티션이 있습니다.
- sortBy: { orderDate: 1 }각 파티션의 문서를- orderDate별로 오름차순(- 1)으로 정렬 하므로 가장 이른- orderDate가 먼저 표시됩니다.
- output:
이 예시 출력에서는 CA 및 WA의 누적 quantity가 cumulativeQuantityForState 필드에 표시됩니다.
{ "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"),   "state" : "CA", "price" : 41, "quantity" : 162, "cumulativeQuantityForState" : 162 } { "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"),   "state" : "CA", "price" : 13, "quantity" : 120, "cumulativeQuantityForState" : 282 } { "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"),   "state" : "CA", "price" : 12, "quantity" : 145, "cumulativeQuantityForState" : 427 } { "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"),   "state" : "WA", "price" : 43, "quantity" : 134, "cumulativeQuantityForState" : 134 } { "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"),   "state" : "WA", "price" : 13, "quantity" : 104, "cumulativeQuantityForState" : 238 } { "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"),   "state" : "WA", "price" : 14, "quantity" : 140, "cumulativeQuantityForState" : 378 } 
문서 창을 사용해 연도별 누적 수량 가져오기
이 예시에서는 $setWindowFields의 문서 창을 사용하여 orderDate의 각 $year에 대한 누적 케이크 판매량quantity(을)를 산출합니다.
db.cakeSales.aggregate( [    {       $setWindowFields: {          partitionBy: { $year: "$orderDate" },          sortBy: { orderDate: 1 },          output: {             cumulativeQuantityForYear: {                $sum: "$quantity",                window: {                   documents: [ "unbounded", "current" ]                }             }          }       }    } ] ) 
예시:
- partitionBy: { $year: "$orderDate" }- orderDate에서- $year를 기준으로 사용해 컬렉션의 문서를 파티셔닝합니다.- 2019,- 2020,- 2021에 대한 파티션이 있습니다.
- sortBy: { orderDate: 1 }각 파티션의 문서를- orderDate별로 오름차순(- 1)으로 정렬 하므로 가장 이른- orderDate가 먼저 표시됩니다.
- output:
이 예시 출력에서는 각 연도의 누적 quantity가 cumulativeQuantityForYear 필드에 표시됩니다.
{ "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"),   "state" : "WA", "price" : 43, "quantity" : 134, "cumulativeQuantityForYear" : 134 } { "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"),   "state" : "CA", "price" : 41, "quantity" : 162, "cumulativeQuantityForYear" : 296 } { "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"),   "state" : "WA", "price" : 13, "quantity" : 104, "cumulativeQuantityForYear" : 104 } { "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"),   "state" : "CA", "price" : 13, "quantity" : 120, "cumulativeQuantityForYear" : 224 } { "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"),   "state" : "CA", "price" : 12, "quantity" : 145, "cumulativeQuantityForYear" : 145 } { "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"),   "state" : "WA", "price" : 14, "quantity" : 140, "cumulativeQuantityForYear" : 285 } 
문서 창을 사용하여 연도별 이동 평균 수량 가져오기
이 예시에서는 $setWindowFields의 문서 창을 사용하여 케이크 판매량 quantity에 대한 이동 평균을 출력합니다.
db.cakeSales.aggregate( [    {       $setWindowFields: {          partitionBy: { $year: "$orderDate" },          sortBy: { orderDate: 1 },          output: {             averageQuantity: {                $avg: "$quantity",                window: {                   documents: [ -1, 0 ]                }             }          }       }    } ] ) 
예시:
- partitionBy: "$orderDate"- orderDate에서- $year를 기준으로 사용해 컬렉션의 문서를 파티셔닝합니다.- 2019,- 2020,- 2021에 대한 파티션이 있습니다.
- sortBy: { orderDate: 1 }각 파티션의 문서를- orderDate별로 오름차순(- 1)으로 정렬 하므로 가장 이른- orderDate가 먼저 표시됩니다.
- output:
이 예시 출력에서는 이동 평균 quantity가 averageQuantity 필드에 표시됩니다.
{ "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"),   "state" : "WA", "price" : 43, "quantity" : 134, "averageQuantity" : 134 } { "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"),   "state" : "CA", "price" : 41, "quantity" : 162, "averageQuantity" : 148 } { "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"),   "state" : "WA", "price" : 13, "quantity" : 104, "averageQuantity" : 104 } { "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"),   "state" : "CA", "price" : 13, "quantity" : 120, "averageQuantity" : 112 } { "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"),   "state" : "CA", "price" : 12, "quantity" : 145, "averageQuantity" : 145 } { "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"),   "state" : "WA", "price" : 14, "quantity" : 140, "averageQuantity" : 142.5 } 
문서 창을 사용하여 연도별 누적 및 최대 수량 가져오기
이 예시에서는 $setWindowFields의 문서 창을 사용하여 각 $year orderDate의 누적 및 최대 케이크 판매 quantity 값을 출력합니다.
db.cakeSales.aggregate( [    {       $setWindowFields: {          partitionBy: { $year: "$orderDate" },          sortBy: { orderDate: 1 },          output: {             cumulativeQuantityForYear: {                $sum: "$quantity",                window: {                   documents: [ "unbounded", "current" ]                }             },             maximumQuantityForYear: {                $max: "$quantity",                window: {                   documents: [ "unbounded", "unbounded" ]                }             }          }       }    } ] ) 
예시:
- partitionBy: "$orderDate"- orderDate에서- $year를 기준으로 사용해 컬렉션의 문서를 파티셔닝합니다.- 2019,- 2020,- 2021에 대한 파티션이 있습니다.
- sortBy: { orderDate: 1 }각 파티션의 문서를- orderDate별로 오름차순(- 1)으로 정렬 하므로 가장 이른- orderDate가 먼저 표시됩니다.
- output:- cumulativeQuantityForYear필드를 각 연도의 누적- quantity(으)로 설정합니다.
- 문서 창에서 실행되는 - $sum연산자를 사용하여 누적- quantity를 계산합니다.- 이 창에는 - unbounded하한과- current문서 사이의 문서가 포함됩니다. 즉,- $sum은 파티션 시작 부분과 현재 문서 사이의 문서에 대한 누적 수량을 반환합니다.
- 연도별로 - maximumQuantityForYear필드를 최대- quantity로 설정합니다.
- 문서 창에서 실행되는 - $max연산자를 사용하여 모든 문서의 최대- quantity를 계산합니다.- 창에는 - unbounded하한과- upper한도 사이의 문서가 포함되어 있습니다. 즉,- $max(은)는 파티션에 있는 문서의 최대 수를 반환합니다.
 
이 예시의 출력에서는 누적된 quantity가 cumulativeQuantityForYear 필드에 표시되고, 최대 quantity는 maximumQuantityForYear 필드에 표시됩니다.
{ "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"),   "state" : "WA", "price" : 43, "quantity" : 134,   "cumulativeQuantityForYear" : 134, "maximumQuantityForYear" : 162 } { "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"),   "state" : "CA", "price" : 41, "quantity" : 162,   "cumulativeQuantityForYear" : 296, "maximumQuantityForYear" : 162 } { "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"),   "state" : "WA", "price" : 13, "quantity" : 104,   "cumulativeQuantityForYear" : 104, "maximumQuantityForYear" : 120 } { "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"),   "state" : "CA", "price" : 13, "quantity" : 120,    "cumulativeQuantityForYear" : 224, "maximumQuantityForYear" : 120 } { "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"),   "state" : "CA", "price" : 12, "quantity" : 145,   "cumulativeQuantityForYear" : 145, "maximumQuantityForYear" : 145 } { "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"),   "state" : "WA", "price" : 14, "quantity" : 140,   "cumulativeQuantityForYear" : 285, "maximumQuantityForYear" : 145 } 
범위 윈도우 예시
이 예시에서는 $setWindowFields의 범위 창을 사용하여 현재 문서의 price 값에서 차액이 10달러 이내인 주문에 대해 판매된 케이크 quantity 값의 합계를 반환합니다.
db.cakeSales.aggregate( [    {       $setWindowFields: {          partitionBy: "$state",          sortBy: { price: 1 },          output: {             quantityFromSimilarOrders: {                $sum: "$quantity",                window: {                   range: [ -10, 10 ]                }             }          }       }    } ] ) 
예시:
- partitionBy: "$state"는 컬렉션의 문서를- state로 파티셔닝합니다.- CA및- WA에 대한 파티션이 있습니다.
- sortBy: { price: 1 }각 파티션의 문서를 오름차순(- 1)- price로 정렬하므로, 가장 낮은- price가 먼저 정렬됩니다.
- output- quantityFromSimilarOrders필드를 범위 창에 있는 문서의- quantity값의 합계로 설정합니다.
이 예시 출력에서는 창에 있는 문서의 quantity 값 합계가 quantityFromSimilarOrders 필드에 표시됩니다.
{ "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"),   "state" : "CA", "price" : 12, "quantity" : 145, "quantityFromSimilarOrders" : 265 } { "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"),   "state" : "CA", "price" : 13, "quantity" : 120, "quantityFromSimilarOrders" : 265 } { "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"),   "state" : "CA", "price" : 41, "quantity" : 162, "quantityFromSimilarOrders" : 162 } { "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"),   "state" : "WA", "price" : 13, "quantity" : 104, "quantityFromSimilarOrders" : 244 } { "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"),   "state" : "WA", "price" : 14, "quantity" : 140, "quantityFromSimilarOrders" : 244 } { "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"),   "state" : "WA", "price" : 43, "quantity" : 134, "quantityFromSimilarOrders" : 134 } 
시간 범위 창 예시
상한이 양수인 시간 범위 창 사용
다음 예시에서는 양수 상한 시간 범위 단위가 $setWindowFields인 창을 사용합니다. 파이프라인은 지정된 시간 범위와 일치하는 각 state에 대해 orderDate 값의 배열을 출력합니다.
db.cakeSales.aggregate( [    {       $setWindowFields: {          partitionBy: "$state",          sortBy: { orderDate: 1 },          output: {             recentOrders: {                $push: "$orderDate",                window: {                   range: [ "unbounded", 10 ],                   unit: "month"                }             }          }       }    } ] ) 
예시:
- partitionBy: "$state"는 컬렉션의 문서를- state로 파티셔닝합니다.- CA및- WA에 대한 파티션이 있습니다.
- sortBy: { orderDate: 1 }각 파티션의 문서를- orderDate별로 오름차순(- 1)으로 정렬 하므로 가장 이른- orderDate가 먼저 표시됩니다.
- output:
- 이 창에는 시간 범위 단위를 사용하여 - unbounded하한과- 10(현재 문서의- orderDate값 이후 10개월)로 설정된 상한 사이의 문서가 포함됩니다.
- $push는 파티션의 시작 부분과 현재 문서의- orderDate값에- 10개월을 더한 범위 내에서- orderDate값을 포함하는 문서 사이에 있는 문서들에 대한- orderDate값 배열을 반환합니다.
이 출력 예시에서 CA와 WA에 대한 orderDate 값 배열은 recentOrders 필드에 표시됩니다.
{ "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"),   "state" : "CA", "price" : 41, "quantity" : 162,   "recentOrders" : [ ISODate("2019-05-18T16:09:01Z") ] } { "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"),   "state" : "CA", "price" : 13, "quantity" : 120,   "recentOrders" : [ ISODate("2019-05-18T16:09:01Z"), ISODate("2020-05-18T14:10:30Z"), ISODate("2021-01-11T06:31:15Z") ] } { "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"),   "state" : "CA", "price" : 12, "quantity" : 145,   "recentOrders" : [ ISODate("2019-05-18T16:09:01Z"), ISODate("2020-05-18T14:10:30Z"), ISODate("2021-01-11T06:31:15Z") ] } { "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"),   "state" : "WA", "price" : 43, "quantity" : 134,   "recentOrders" : [ ISODate("2019-01-08T06:12:03Z") ] } { "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"),   "state" : "WA", "price" : 13, "quantity" : 104,   "recentOrders" : [ ISODate("2019-01-08T06:12:03Z"), ISODate("2020-02-08T13:13:23Z") ] } { "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"),   "state" : "WA", "price" : 14, "quantity" : 140,   "recentOrders" : [ ISODate("2019-01-08T06:12:03Z"), ISODate("2020-02-08T13:13:23Z"), ISODate("2021-03-20T11:30:05Z") ] } 
음수 상한이 있는 시간 범위 창 사용
다음 예시에서는 음수 상한 시간 범위 단위가 $setWindowFields인 창을 사용합니다. 파이프라인은 지정된 시간 범위와 일치하는 각 state에 대해 orderDate 값의 배열을 출력합니다.
db.cakeSales.aggregate( [    {       $setWindowFields: {          partitionBy: "$state",          sortBy: { orderDate: 1 },          output: {             recentOrders: {                $push: "$orderDate",                window: {                   range: [ "unbounded", -10 ],                   unit: "month"                }             }          }       }    } ] ) 
예시:
- partitionBy: "$state"는 컬렉션의 문서를- state로 파티셔닝합니다.- CA및- WA에 대한 파티션이 있습니다.
- sortBy: { orderDate: 1 }각 파티션의 문서를- orderDate별로 오름차순(- 1)으로 정렬 하므로 가장 이른- orderDate가 먼저 표시됩니다.
- output:
- 창에는 시간 범위 단위를 사용하여 - unbounded하한 및- -10(현재 문서의- orderDate값보다 10개월 전)으로 설정된 상한 사이의 문서가 포함됩니다.
- $push는 파티션의 시작 부분과 현재 문서의- orderDate값에서- 10개월을 뺀 범위 내에서- orderDate값을 포함하는 문서 사이에 있는 문서들에 대한- orderDate값 배열을 반환합니다.
이 출력 예시에서 CA와 WA에 대한 orderDate 값 배열은 recentOrders 필드에 표시됩니다.
{ "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"),   "state" : "CA", "price" : 41, "quantity" : 162,   "recentOrders" : [ ] } { "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"),   "state" : "CA", "price" : 13, "quantity" : 120,   "recentOrders" : [ ISODate("2019-05-18T16:09:01Z") ] } { "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"),   "state" : "CA", "price" : 12, "quantity" : 145,   "recentOrders" : [ ISODate("2019-05-18T16:09:01Z") ] } { "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"),   "state" : "WA", "price" : 43, "quantity" : 134,   "recentOrders" : [ ] } { "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"),   "state" : "WA", "price" : 13, "quantity" : 104,   "recentOrders" : [ ISODate("2019-01-08T06:12:03Z") ] } { "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"),   "state" : "WA", "price" : 14, "quantity" : 140,   "recentOrders" : [ ISODate("2019-01-08T06:12:03Z"), ISODate("2020-02-08T13:13:23Z") ] } 
다음 WeatherMeasurement 클래스는 날씨 측정값 컬렉션 의 문서를 나타냅니다.
public class WeatherMeasurement {     public Guid Id { get; set; }          public string LocalityId { get; set; }          public DateTime MeasurementDateTime { get; set; }        public float Rainfall { get; set; }          public float Temperature { get; set; } } 
MongoDB .NET/ C# 운전자 사용하여 $setWindowFields 집계 파이프라인 에 단계를  추가하려면 PipelineDefinition 객체 에서 SetWindowFields() 메서드를 호출합니다.
다음 예시 Rainfall 및 Temperature 필드를 사용하여 각 지역의 지난 한 달 동안 누적 강우량과 이동 평균 온도를 계산하는 파이프라인 단계를 만듭니다.
var pipeline = new EmptyPipelineDefinition<WeatherMeasurement>()     .SetWindowFields(         partitionBy: w => w.LocalityId,         sortBy: Builders<WeatherMeasurement>.Sort.Ascending(             w => w.MeasurementDateTime),         output: o => new         {             MonthlyRainfall = o.Sum(                 w => w.Rainfall, RangeWindow.Create(                     RangeWindow.Months(-1),                     RangeWindow.Current)             ),             TemperatureAvg = o.Average(                 w => w.Temperature, RangeWindow.Create(                     RangeWindow.Months(-1),                     RangeWindow.Current)             )         }     ); 
이 페이지의 Node.js 예시에서는 Atlas 샘플 데이터 세트의 sample_weatherdata.data 컬렉션을 사용합니다. 무료 MongoDB Atlas 클러스터를 생성하고 샘플 데이터 세트를 로드하는 방법을 학습하려면 MongoDB Node.js 드라이버 문서의 시작하기를 참조하세요.
MongoDB Node.js 운전자 사용하여 집계 파이프라인 에 $setWindowFields 단계를 추가하려면 파이프라인 객체 에서 $setWindowFields 연산자 사용합니다.
다음 예시는 지난 한 달 동안 고유한 각 callLetters 값에 대해 airTemperature.value의 평균과 waveMeasurement.waves.height의 총계를 계산하는 파이프라인 단계를 만듭니다. 그리고 집계 파이프라인을 실행합니다.
const pipeline = [   {     $setWindowFields: {       partitionBy: "$callLetters",       sortBy: { ts: 1 },       output: {         temperatureAvg: {           $avg: "$airTemperature.value",           window: {             range: [-1, "current"],             unit: "month"           }         },         totalWaveHeight: {           $sum: "$waveMeasurement.waves.height",           window: {             range: [-1, "current"],             unit: "month"           }         }       }     }   }, ]; const cursor = collection.aggregate(pipeline); return cursor; 
팁
IOT 전력 소비에 대한 추가 예시는 실용적인 MongoDB 집계 e-book을 참조하세요.