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

$setWindowFields(集計ステージ)

$setWindowFields

バージョン 5.0 の新機能。

Performs operations on a specified span of documents in a collection, known as a window, and returns the results based on the chosen window operator.

For example, you can use the $setWindowFields stage to output the:

  • コレクション内の 2 つのドキュメント間の売上の差

  • 売上ランキング

  • 累計販売合計

  • データを外部データベースにエクスポートせずに、複雑な時系列情報の分析

The $setWindowFields stage syntax:

{
$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>: { ... }
}
}
}

The $setWindowFields stage takes a document with these fields:

フィールド
必要性
説明

任意

Specifies an expression to group the documents. In the $setWindowFields stage, the group of documents is known as a partition. Default is one partition for the entire collection.

Required for some operators (see Restrictions)

パーティション内のドキュメントを並べ替えるフィールドを指定します。$sort ステージと同じ構文を使用します。デフォルトではソートされません。

必須

Specifies the field(s) to append to the documents in the output returned by the $setWindowFields stage. Each field is set to the result returned by the window operator.

A field can contain dots to specify embedded document fields and array fields. The semantics for the embedded document dotted notation in the $setWindowFields stage are the same as the $addFields and $set stages. See embedded document $addFields example and embedded document $set example.

任意

ウィンドウの境界とパラメータを指定します。ウィンドウの境界は包括的です。デフォルトは、パーティション内のすべてのドキュメントが含まれる無制限のウィンドウです。

ドキュメントまたは範囲ウィンドウのいずれかを指定します。

任意

コレクションから読み取られた現在のドキュメントの位置を基準にして下限と上限が指定されるウィンドウ。

ウィンドウの境界は、下限と上限の文字列または整数を含む 2 つの要素の配列を使用して指定されます。次を使用します。

  • 出力内の現在のドキュメント位置の "current" 文字列。

  • パーティション内の最初または最後のドキュメント位置を表す "unbounded" 文字列。

  • 現在のドキュメントに対する相対的な位置を表す整数。現在のドキュメントの前の位置には負の整数を使用します。現在のドキュメントの後の位置には正の整数を使用します。0 は現在のドキュメントの位置です。

「ドキュメント ウィンドウの例」を参照してください。

任意

A window where the lower and upper boundaries are defined using a range of values based on the sortBy field in the current document.

ウィンドウの境界は、下限と上限の文字列または数値を含む 2 つの要素の配列を使用して指定されます。次を使用します。

  • 出力内の現在のドキュメント位置の "current" 文字列。

  • パーティション内の最初または最後のドキュメント位置を表す "unbounded" 文字列。

  • 現在のドキュメントの sortBy フィールドの値に追加する数値。sortBy フィールドの値が下限と上限の範囲内にある場合、ドキュメントはウィンドウ内にあります。

「範囲ウィンドウの例」を参照してください。

任意

時間の範囲ウィンドウ境界の単位を指定します。次のいずれかの文字列に設定できます。

  • "year"

  • "quarter"

  • "month"

  • "week"

  • "day"

  • "hour"

  • "minute"

  • "second"

  • "millisecond"

If omitted, default numeric range window boundaries are used.

「時間の範囲ウィンドウの例」を参照してください。

Tip

例

The $setWindowFields stage appends new fields to existing documents. You can include one or more $setWindowFields stages in an aggregation operation.

Starting in MongoDB 5.3, you can use the $setWindowFields stage with transactions and the "snapshot" read concern.

$setWindowFields ステージでは、返されるドキュメントの順序は保証されません。

These operators can be used with the $setWindowFields stage:

バージョン8.3の新機能。

$setWindowsFields操作で explain を実行すると、出力には、使用中の追跡メモリの最大バイト数を含む peakTrackedMemBytesフィールドが含まれます。詳しくは、explain.executionStats.peakTrackedMemBytes を参照してください。

Restrictions for the $setWindowFields stage:

  • Before MongoDB 5.3, the $setWindowFields stage cannot be used:

  • sortBy is required for:

  • 範囲ウィンドウでは、すべての sortBy 値が数値である必要があります。

  • 時間範囲Windows 、すべてのsortBy値が日付である必要があります。

  • 範囲ウィンドウと時間範囲ウィンドウには sortBy フィールドを 1 つだけ含めることができ、並べ替えは昇順でなければなりません。

  • You cannot specify both a documents window and a range window.

  • これらの演算子は暗黙的なウィンドウを使用し、ウィンドウオプションを指定するとエラーを返します。

  • For range windows, only numbers in the specified range are included in the window. Missing, undefined, and null values are excluded.

  • For time range windows:

    • ウィンドウには日付と時刻の種類のみが含まれます。

    • 数値境界値は整数である必要があります。たとえば、境界として 2 時間を使用できますが、1.5 時間は使用できません。

  • 空のウィンドウまたは互換性のない値を持つウィンドウ(例: 文字列で $sum を使用)の場合、返される値は演算子によって異なります。

    • $count および $sum の場合、返される値は 0 です。

    • $addToSet および $push の場合、返される値は空の配列になります。

    • その他のすべての演算子の場合、返される値は null です。

このページの例では、sample_mflix データセット のデータを使用しています。自己管理型 MongoDB 配置にこのデータセットをロードする方法の詳細については、「サンプル データセットのロード」を参照してください。サンプル データベースに何らかの変更を加えた場合は、今ページの例を実行するには、データベースを削除して再作成する必要がある可能性があります。

この例では、 $setWindowFieldsruntimeratedのドキュメントウィンドウを使用して、 値ごとに累積 を出力します。出力例では、cumulativeRuntimeForRating runtimeフィールドには各評価の累積 が表示されます。

db.movies.aggregate( [
{ $match: { metacritic: { $gt: 85 }, year: { $gte: 1993, $lte: 1995 } } },
{ $project: { _id: 0, title: 1, rated: 1, released: 1, runtime: 1 } },
{
$setWindowFields: {
partitionBy: "$rated",
sortBy: { released: 1 },
output: {
cumulativeRuntimeForRating: {
$sum: "$runtime",
window: {
documents: [ "unbounded", "current" ]
}
}
}
}
}
] )
[
{
rated: 'G',
title: 'Toy Story',
released: ISODate('1995-11-22T00:00:00.000Z'),
runtime: 81,
cumulativeRuntimeForRating: 81
},
{
rated: 'PG',
title: 'Little Women',
released: ISODate('1994-12-25T00:00:00.000Z'),
runtime: 115,
cumulativeRuntimeForRating: 115
},
{
rated: 'PG-13',
title: 'The Fugitive',
released: ISODate('1993-08-06T00:00:00.000Z'),
runtime: 130,
cumulativeRuntimeForRating: 130
},
{
rated: 'PG-13',
title: 'King of the Hill',
released: ISODate('1993-08-20T00:00:00.000Z'),
runtime: 103,
cumulativeRuntimeForRating: 233
},
{
runtime: 133,
rated: 'PG-13',
title: 'Quiz Show',
released: ISODate('1994-10-07T00:00:00.000Z'),
cumulativeRuntimeForRating: 366
},
{
rated: 'PG-13',
title: 'Hoop Dreams',
released: ISODate('1994-10-14T00:00:00.000Z'),
runtime: 170,
cumulativeRuntimeForRating: 536
},
{
runtime: 195,
rated: 'R',
title: "Schindler's List",
released: ISODate('1994-02-04T00:00:00.000Z'),
cumulativeRuntimeForRating: 195
},
{
runtime: 121,
rated: 'R',
title: 'The Piano',
released: ISODate('1994-02-11T00:00:00.000Z'),
cumulativeRuntimeForRating: 316
},
{
rated: 'R',
title: 'Pulp Fiction',
released: ISODate('1994-10-14T00:00:00.000Z'),
runtime: 154,
cumulativeRuntimeForRating: 470
},
{
runtime: 119,
title: 'Crumb',
released: ISODate('1995-04-28T00:00:00.000Z'),
rated: 'R',
cumulativeRuntimeForRating: 589
},
{
rated: 'R',
title: 'To Die For',
released: ISODate('1995-10-06T00:00:00.000Z'),
runtime: 106,
cumulativeRuntimeForRating: 695
}
]

この例では、次のことが行われます。

  • partitionBy: "$rated" はコレクション内のドキュメントをrated で 分割 します。パーティションは、G PG、PG-13 、 、R です。

  • sortBy: { released: 1 } では、各パーティション内のドキュメントをreleased で昇順(1 )にソートするため、最も近いreleased 日付が先頭になります。

  • output:

    • cumulativeRuntimeForRatingフィールドを各 rated 値の累積 runtime に設定します。これは、パーティション内の前の値に連続して追加されることによって増加します。

    • Calculates the cumulative runtime using the $sum operator run in a documents window.

      The window contains documents between an unbounded lower limit and the current document. This means $sum returns the cumulative runtime for the documents between the beginning of the partition and the current document.

この例では、 $setWindowFieldsのドキュメントウィンドウを使用して、runtime の各$year の累積released を出力します。出力の例では、cumulativeRuntimeForYear runtimeフィールドには各年の累積 が表示されます。

db.movies.aggregate( [
{ $match: { metacritic: { $gt: 85 }, year: { $gte: 1993, $lte: 1995 } } },
{ $project: { _id: 0, title: 1, released: 1, runtime: 1 } },
{
$setWindowFields: {
partitionBy: { $year: "$released" },
sortBy: { released: 1 },
output: {
cumulativeRuntimeForYear: {
$sum: "$runtime",
window: {
documents: [ "unbounded", "current" ]
}
}
}
}
}
] )
[
{
title: 'The Fugitive',
released: ISODate('1993-08-06T00:00:00.000Z'),
runtime: 130,
cumulativeRuntimeForYear: 130
},
{
title: 'King of the Hill',
released: ISODate('1993-08-20T00:00:00.000Z'),
runtime: 103,
cumulativeRuntimeForYear: 233
},
{
runtime: 195,
title: "Schindler's List",
released: ISODate('1994-02-04T00:00:00.000Z'),
cumulativeRuntimeForYear: 195
},
{
runtime: 121,
title: 'The Piano',
released: ISODate('1994-02-11T00:00:00.000Z'),
cumulativeRuntimeForYear: 316
},
{
runtime: 133,
title: 'Quiz Show',
released: ISODate('1994-10-07T00:00:00.000Z'),
cumulativeRuntimeForYear: 449
},
{
title: 'Hoop Dreams',
released: ISODate('1994-10-14T00:00:00.000Z'),
runtime: 170,
cumulativeRuntimeForYear: 619
},
{
title: 'Pulp Fiction',
released: ISODate('1994-10-14T00:00:00.000Z'),
runtime: 154,
cumulativeRuntimeForYear: 773
},
{
title: 'Little Women',
released: ISODate('1994-12-25T00:00:00.000Z'),
runtime: 115,
cumulativeRuntimeForYear: 888
},
{
runtime: 119,
title: 'Crumb',
released: ISODate('1995-04-28T00:00:00.000Z'),
cumulativeRuntimeForYear: 119
},
{
title: 'To Die For',
released: ISODate('1995-10-06T00:00:00.000Z'),
runtime: 106,
cumulativeRuntimeForYear: 225
},
{
title: 'Toy Story',
released: ISODate('1995-11-22T00:00:00.000Z'),
runtime: 81,
cumulativeRuntimeForYear: 306
}
]

この例では、次のことが行われます。

  • partitionBy: { $year: "$released" } partitions the documents in the collection by $year in released. There are partitions for 1993, 1994, and 1995.

  • sortBy: { released: 1 } では、各パーティション内のドキュメントをreleased で昇順(1 )にソートするため、最も近いreleased 日付が先頭になります。

  • output:

    • cumulativeRuntimeForYear フィールドを各年の累積 runtime に設定します。これは、パーティション内の前の値に連続して追加されることによって増加します。

    • Calculates the cumulative runtime using the $sum operator run in a documents window.

      The window contains documents between an unbounded lower limit and the current document. This means $sum returns the cumulative runtime for the documents between the beginning of the partition and the current document.

この例では $setWindowFieldsのドキュメントウィンドウを使用して、移動平均runtime を出力します。出力の例では、averageRuntime フィールドには移動平均runtime が表示されます。

db.movies.aggregate( [
{ $match: { metacritic: { $gt: 85 }, year: { $gte: 1993, $lte: 1995 } } },
{ $project: { _id: 0, title: 1, released: 1, runtime: 1 } },
{
$setWindowFields: {
partitionBy: { $year: "$released" },
sortBy: { released: 1 },
output: {
averageRuntime: {
$avg: "$runtime",
window: {
documents: [ -1, 0 ]
}
}
}
}
}
] )
[
{
title: 'The Fugitive',
released: ISODate('1993-08-06T00:00:00.000Z'),
runtime: 130,
averageRuntime: 130
},
{
title: 'King of the Hill',
released: ISODate('1993-08-20T00:00:00.000Z'),
runtime: 103,
averageRuntime: 116.5
},
{
runtime: 195,
title: "Schindler's List",
released: ISODate('1994-02-04T00:00:00.000Z'),
averageRuntime: 195
},
{
runtime: 121,
title: 'The Piano',
released: ISODate('1994-02-11T00:00:00.000Z'),
averageRuntime: 158
},
{
runtime: 133,
title: 'Quiz Show',
released: ISODate('1994-10-07T00:00:00.000Z'),
averageRuntime: 127
},
{
title: 'Hoop Dreams',
released: ISODate('1994-10-14T00:00:00.000Z'),
runtime: 170,
averageRuntime: 151.5
},
{
title: 'Pulp Fiction',
released: ISODate('1994-10-14T00:00:00.000Z'),
runtime: 154,
averageRuntime: 162
},
{
title: 'Little Women',
released: ISODate('1994-12-25T00:00:00.000Z'),
runtime: 115,
averageRuntime: 134.5
},
{
runtime: 119,
title: 'Crumb',
released: ISODate('1995-04-28T00:00:00.000Z'),
averageRuntime: 119
},
{
title: 'To Die For',
released: ISODate('1995-10-06T00:00:00.000Z'),
runtime: 106,
averageRuntime: 112.5
},
{
title: 'Toy Story',
released: ISODate('1995-11-22T00:00:00.000Z'),
runtime: 81,
averageRuntime: 93.5
}
]

この例では、次のことが行われます。

  • partitionBy: { $year: "$released" } partitions the documents in the collection by $year in released. There are partitions for 1993, 1994, and 1995.

  • sortBy: { released: 1 } では、各パーティション内のドキュメントをreleased で昇順(1 )にソートするため、最も近いreleased 日付が先頭になります。

  • output:

    • averageRuntime フィールドを各年の移動平均 runtime に設定します。

    • Calculates the moving average runtime using the $avg operator run in a documents window.

      The window contains documents between -1 and 0. This means $avg returns the moving average runtime between the document before the current document (-1) and the current document (0) in the partition.

この例では、 $setWindowFieldsruntime$yearreleasedのドキュメントウィンドウを使用して、 の各 の累計値と最大 値を出力します。出力例では、cumulativeRuntimeForYear フィールドには累積runtime が表示され、maximumRuntimeForYear フィールドには最大のruntime が表示されます。

db.movies.aggregate( [
{ $match: { metacritic: { $gt: 85 }, year: { $gte: 1993, $lte: 1995 } } },
{ $project: { _id: 0, title: 1, released: 1, runtime: 1 } },
{
$setWindowFields: {
partitionBy: { $year: "$released" },
sortBy: { released: 1 },
output: {
cumulativeRuntimeForYear: {
$sum: "$runtime",
window: {
documents: [ "unbounded", "current" ]
}
},
maximumRuntimeForYear: {
$max: "$runtime",
window: {
documents: [ "unbounded", "unbounded" ]
}
}
}
}
}
] )
[
{
title: 'The Fugitive',
released: ISODate('1993-08-06T00:00:00.000Z'),
runtime: 130,
cumulativeRuntimeForYear: 130,
maximumRuntimeForYear: 130
},
{
title: 'King of the Hill',
released: ISODate('1993-08-20T00:00:00.000Z'),
runtime: 103,
cumulativeRuntimeForYear: 233,
maximumRuntimeForYear: 130
},
{
runtime: 195,
title: "Schindler's List",
released: ISODate('1994-02-04T00:00:00.000Z'),
cumulativeRuntimeForYear: 195,
maximumRuntimeForYear: 195
},
{
runtime: 121,
title: 'The Piano',
released: ISODate('1994-02-11T00:00:00.000Z'),
cumulativeRuntimeForYear: 316,
maximumRuntimeForYear: 195
},
{
runtime: 133,
title: 'Quiz Show',
released: ISODate('1994-10-07T00:00:00.000Z'),
cumulativeRuntimeForYear: 449,
maximumRuntimeForYear: 195
},
{
title: 'Hoop Dreams',
released: ISODate('1994-10-14T00:00:00.000Z'),
runtime: 170,
cumulativeRuntimeForYear: 619,
maximumRuntimeForYear: 195
},
{
title: 'Pulp Fiction',
released: ISODate('1994-10-14T00:00:00.000Z'),
runtime: 154,
cumulativeRuntimeForYear: 773,
maximumRuntimeForYear: 195
},
{
title: 'Little Women',
released: ISODate('1994-12-25T00:00:00.000Z'),
runtime: 115,
cumulativeRuntimeForYear: 888,
maximumRuntimeForYear: 195
},
{
runtime: 119,
title: 'Crumb',
released: ISODate('1995-04-28T00:00:00.000Z'),
cumulativeRuntimeForYear: 119,
maximumRuntimeForYear: 119
},
{
title: 'To Die For',
released: ISODate('1995-10-06T00:00:00.000Z'),
runtime: 106,
cumulativeRuntimeForYear: 225,
maximumRuntimeForYear: 119
},
{
title: 'Toy Story',
released: ISODate('1995-11-22T00:00:00.000Z'),
runtime: 81,
cumulativeRuntimeForYear: 306,
maximumRuntimeForYear: 119
}
]

この例では、次のことが行われます。

  • partitionBy: { $year: "$released" } partitions the documents in the collection by $year in released. There are partitions for 1993, 1994, and 1995.

  • sortBy: { released: 1 } では、各パーティション内のドキュメントをreleased で昇順(1 )にソートするため、最も近いreleased 日付が先頭になります。

  • output:

    • cumulativeRuntimeForYear フィールドを各年の累積 runtime に設定します。

    • Calculates the cumulative runtime using the $sum operator run in a documents window.

      The window contains documents between an unbounded lower limit and the current document. This means $sum returns the cumulative runtime for the documents between the beginning of the partition and the current document.

    • maximumRuntimeForYear フィールドを各年の最大値 runtime に設定します。

    • Calculates the maximum runtime of all the documents using the $max operator run in a documents window.

      ウィンドウ には、unbounded 下限からupper の上限の間のドキュメントが含まれています。つまり、$max runtimeはパーティション内のドキュメントに対して最大の を返します。

この例では、 $setWindowFieldsruntime10の範囲ウィンドウを使用して、現在のドキュメントのruntime 値のプラスマイナス 分以内にリリースされた映画の 値の合計を返します。出力例では、runtimeFromSimilarMovies フィールドにはウィンドウ内のドキュメントのruntime 値の合計が表示されます。

db.movies.aggregate( [
{ $match: { metacritic: { $gt: 85 }, year: { $gte: 1993, $lte: 1995 } } },
{ $project: { _id: 0, title: 1, rated: 1, runtime: 1 } },
{
$setWindowFields: {
partitionBy: "$rated",
sortBy: { runtime: 1 },
output: {
runtimeFromSimilarMovies: {
$sum: "$runtime",
window: {
range: [ -10, 10 ]
}
}
}
}
}
] )
[
{
rated: 'G',
title: 'Toy Story',
runtime: 81,
runtimeFromSimilarMovies: 81
},
{
rated: 'PG',
title: 'Little Women',
runtime: 115,
runtimeFromSimilarMovies: 115
},
{
rated: 'PG-13',
title: 'King of the Hill',
runtime: 103,
runtimeFromSimilarMovies: 103
},
{
rated: 'PG-13',
title: 'The Fugitive',
runtime: 130,
runtimeFromSimilarMovies: 263
},
{
runtime: 133,
rated: 'PG-13',
title: 'Quiz Show',
runtimeFromSimilarMovies: 263
},
{
rated: 'PG-13',
title: 'Hoop Dreams',
runtime: 170,
runtimeFromSimilarMovies: 170
},
{
rated: 'R',
title: 'To Die For',
runtime: 106,
runtimeFromSimilarMovies: 106
},
{
runtime: 119,
title: 'Crumb',
rated: 'R',
runtimeFromSimilarMovies: 240
},
{
runtime: 121,
rated: 'R',
title: 'The Piano',
runtimeFromSimilarMovies: 240
},
{
rated: 'R',
title: 'Pulp Fiction',
runtime: 154,
runtimeFromSimilarMovies: 154
},
{
runtime: 195,
rated: 'R',
title: "Schindler's List",
runtimeFromSimilarMovies: 195
}
]

この例では、次のことが行われます。

  • partitionBy: "$rated" はコレクション内のドキュメントをrated で 分割 します。パーティションは、G PG、PG-13 、 、R です。

  • sortBy: { runtime: 1 } は各パーティション内のドキュメントをruntime で昇順(1 )にソートするため、最も短いruntime が先頭になります。

  • output runtimeFromSimilarMovies フィールドを、範囲 ウィンドウ内のドキュメントの runtime 値の合計に設定します。

    • The window contains documents between a lower limit of -10 and an upper limit of 10. The range is inclusive.

    • $sumruntime10は、現在のドキュメントのruntime 値のプラスマイナス 分の範囲に含まれる 値の合計を返します。

次の例では、 $setWindowFieldsreleasedratedの上限時間範囲単位が正のウィンドウを使用します。パイプラインは、指定された時間範囲に一致する 値ごとに 値の配列を出力します。出力例では、recentReleases フィールドにreleased 値の配列が表示されます。

db.movies.aggregate( [
{ $match: { metacritic: { $gt: 85 }, year: { $gte: 1993, $lte: 1995 } } },
{ $project: { _id: 0, title: 1, rated: 1, released: 1 } },
{
$setWindowFields: {
partitionBy: "$rated",
sortBy: { released: 1 },
output: {
recentReleases: {
$push: "$released",
window: {
range: [ "unbounded", 10 ],
unit: "month"
}
}
}
}
}
] )
[
{
rated: 'G',
title: 'Toy Story',
released: ISODate('1995-11-22T00:00:00.000Z'),
recentReleases: [
ISODate('1995-11-22T00:00:00.000Z')
]
},
{
rated: 'PG',
title: 'Little Women',
released: ISODate('1994-12-25T00:00:00.000Z'),
recentReleases: [
ISODate('1994-12-25T00:00:00.000Z')
]
},
{
rated: 'PG-13',
title: 'The Fugitive',
released: ISODate('1993-08-06T00:00:00.000Z'),
recentReleases: [
ISODate('1993-08-06T00:00:00.000Z'),
ISODate('1993-08-20T00:00:00.000Z')
]
},
{
rated: 'PG-13',
title: 'King of the Hill',
released: ISODate('1993-08-20T00:00:00.000Z'),
recentReleases: [
ISODate('1993-08-06T00:00:00.000Z'),
ISODate('1993-08-20T00:00:00.000Z')
]
},
{
rated: 'PG-13',
title: 'Quiz Show',
released: ISODate('1994-10-07T00:00:00.000Z'),
recentReleases: [
ISODate('1993-08-06T00:00:00.000Z'),
ISODate('1993-08-20T00:00:00.000Z'),
ISODate('1994-10-07T00:00:00.000Z'),
ISODate('1994-10-14T00:00:00.000Z')
]
},
{
rated: 'PG-13',
title: 'Hoop Dreams',
released: ISODate('1994-10-14T00:00:00.000Z'),
recentReleases: [
ISODate('1993-08-06T00:00:00.000Z'),
ISODate('1993-08-20T00:00:00.000Z'),
ISODate('1994-10-07T00:00:00.000Z'),
ISODate('1994-10-14T00:00:00.000Z')
]
},
{
rated: 'R',
title: "Schindler's List",
released: ISODate('1994-02-04T00:00:00.000Z'),
recentReleases: [
ISODate('1994-02-04T00:00:00.000Z'),
ISODate('1994-02-11T00:00:00.000Z'),
ISODate('1994-10-14T00:00:00.000Z')
]
},
{
rated: 'R',
title: 'The Piano',
released: ISODate('1994-02-11T00:00:00.000Z'),
recentReleases: [
ISODate('1994-02-04T00:00:00.000Z'),
ISODate('1994-02-11T00:00:00.000Z'),
ISODate('1994-10-14T00:00:00.000Z')
]
},
{
rated: 'R',
title: 'Pulp Fiction',
released: ISODate('1994-10-14T00:00:00.000Z'),
recentReleases: [
ISODate('1994-02-04T00:00:00.000Z'),
ISODate('1994-02-11T00:00:00.000Z'),
ISODate('1994-10-14T00:00:00.000Z'),
ISODate('1995-04-28T00:00:00.000Z')
]
},
{
title: 'Crumb',
released: ISODate('1995-04-28T00:00:00.000Z'),
rated: 'R',
recentReleases: [
ISODate('1994-02-04T00:00:00.000Z'),
ISODate('1994-02-11T00:00:00.000Z'),
ISODate('1994-10-14T00:00:00.000Z'),
ISODate('1995-04-28T00:00:00.000Z'),
ISODate('1995-10-06T00:00:00.000Z')
]
},
{
rated: 'R',
title: 'To Die For',
released: ISODate('1995-10-06T00:00:00.000Z'),
recentReleases: [
ISODate('1994-02-04T00:00:00.000Z'),
ISODate('1994-02-11T00:00:00.000Z'),
ISODate('1994-10-14T00:00:00.000Z'),
ISODate('1995-04-28T00:00:00.000Z'),
ISODate('1995-10-06T00:00:00.000Z')
]
}
]

この例では、次のことが行われます。

  • partitionBy: "$rated" はコレクション内のドキュメントをrated で 分割 します。パーティションは、G PG、PG-13 、 、R です。

  • sortBy: { released: 1 } では、各パーティション内のドキュメントをreleased で昇順(1 )にソートするため、最も近いreleased 日付が先頭になります。

  • output:

    • 各 rated パーティション内のドキュメントの released 値の配列に recentReleasesフィールドを設定します。配列要素は、配列内の前の要素に追加されて拡張されます。

    • $push を使用して、範囲ウィンドウのドキュメントから released の値の配列を返します。

  • The window contains documents between an unbounded lower limit and an upper limit set to 10 (10 months after the current document's released value) using a time range unit.

  • $push は、パーティションの先頭から、現在のドキュメントの released 値に 10 か月を加えた範囲に含まれる released 値を持つドキュメントまでの間のドキュメントの released 値の配列を返します。

次の例では、 $setWindowFieldsreleasedratedの負の上限時間範囲ユニットを持つウィンドウを使用します。パイプラインは、指定された時間範囲に一致する 値ごとに 値の配列を出力します。出力例では、recentReleases フィールドにreleased 値の配列が表示されます。

db.movies.aggregate( [
{ $match: { metacritic: { $gt: 85 }, year: { $gte: 1993, $lte: 1995 } } },
{ $project: { _id: 0, title: 1, rated: 1, released: 1 } },
{
$setWindowFields: {
partitionBy: "$rated",
sortBy: { released: 1 },
output: {
recentReleases: {
$push: "$released",
window: {
range: [ "unbounded", -10 ],
unit: "month"
}
}
}
}
}
] )
[
{
rated: 'G',
title: 'Toy Story',
released: ISODate('1995-11-22T00:00:00.000Z'),
recentReleases: []
},
{
rated: 'PG',
title: 'Little Women',
released: ISODate('1994-12-25T00:00:00.000Z'),
recentReleases: []
},
{
rated: 'PG-13',
title: 'The Fugitive',
released: ISODate('1993-08-06T00:00:00.000Z'),
recentReleases: []
},
{
rated: 'PG-13',
title: 'King of the Hill',
released: ISODate('1993-08-20T00:00:00.000Z'),
recentReleases: []
},
{
rated: 'PG-13',
title: 'Quiz Show',
released: ISODate('1994-10-07T00:00:00.000Z'),
recentReleases: [
ISODate('1993-08-06T00:00:00.000Z'),
ISODate('1993-08-20T00:00:00.000Z')
]
},
{
rated: 'PG-13',
title: 'Hoop Dreams',
released: ISODate('1994-10-14T00:00:00.000Z'),
recentReleases: [
ISODate('1993-08-06T00:00:00.000Z'),
ISODate('1993-08-20T00:00:00.000Z')
]
},
{
rated: 'R',
title: "Schindler's List",
released: ISODate('1994-02-04T00:00:00.000Z'),
recentReleases: []
},
{
rated: 'R',
title: 'The Piano',
released: ISODate('1994-02-11T00:00:00.000Z'),
recentReleases: []
},
{
rated: 'R',
title: 'Pulp Fiction',
released: ISODate('1994-10-14T00:00:00.000Z'),
recentReleases: []
},
{
title: 'Crumb',
released: ISODate('1995-04-28T00:00:00.000Z'),
rated: 'R',
recentReleases: [
ISODate('1994-02-04T00:00:00.000Z'),
ISODate('1994-02-11T00:00:00.000Z')
]
},
{
rated: 'R',
title: 'To Die For',
released: ISODate('1995-10-06T00:00:00.000Z'),
recentReleases: [
ISODate('1994-02-04T00:00:00.000Z'),
ISODate('1994-02-11T00:00:00.000Z'),
ISODate('1994-10-14T00:00:00.000Z')
]
}
]

この例では、次のことが行われます。

  • partitionBy: "$rated" はコレクション内のドキュメントをrated で 分割 します。パーティションは、G PG、PG-13 、 、R です。

  • sortBy: { released: 1 } では、各パーティション内のドキュメントをreleased で昇順(1 )にソートするため、最も近いreleased 日付が先頭になります。

  • output:

    • 各 rated パーティション内のドキュメントの released 値の配列に recentReleasesフィールドを設定します。配列要素は、配列内の前の要素に追加されて拡張されます。

    • $push を使用して、範囲ウィンドウのドキュメントから released の値の配列を返します。

  • The window contains documents between an unbounded lower limit and an upper limit set to -10 (10 months before the current document's released value) using a time range unit.

  • $push は、パーティションの先頭から、現在のドキュメントの released 値から 10 か月を引いた範囲に含まれる released 値を持つドキュメントまでの間のドキュメントの released 値の配列を返します。

次の例では、$setWindowFields と $shift 演算子および $set ステージを使用して、コレクション内のフィールドを比較します。出力例では、runtimeComparison フィールドに比較結果が表示されます。

db.movies.aggregate( [
{ $match: { metacritic: { $gt: 85 }, year: { $gte: 1993, $lte: 1995 } } },
{ $project: { _id: 0, title: 1, rated: 1, released: 1, runtime: 1 } },
{
$setWindowFields: {
partitionBy: "$rated",
sortBy: { released: 1 },
output: {
previousRuntime: {
$shift: {
output: "$runtime",
by: -1
}
}
}
}
},
{
$set: {
runtimeComparison: {
$cond: [
{ $eq: ["$runtime", "$previousRuntime"] },
"same",
{
$cond: [
{ $gt: ["$runtime", "$previousRuntime"] },
"longer",
"shorter"
]
}
]
}
}
},
] )
[
{
rated: 'G',
title: 'Toy Story',
released: ISODate('1995-11-22T00:00:00.000Z'),
runtime: 81,
previousRuntime: null,
runtimeComparison: 'longer'
},
{
rated: 'PG',
title: 'Little Women',
released: ISODate('1994-12-25T00:00:00.000Z'),
runtime: 115,
previousRuntime: null,
runtimeComparison: 'longer'
},
{
rated: 'PG-13',
title: 'The Fugitive',
released: ISODate('1993-08-06T00:00:00.000Z'),
runtime: 130,
previousRuntime: null,
runtimeComparison: 'longer'
},
{
rated: 'PG-13',
title: 'King of the Hill',
released: ISODate('1993-08-20T00:00:00.000Z'),
runtime: 103,
previousRuntime: 130,
runtimeComparison: 'shorter'
},
{
runtime: 133,
rated: 'PG-13',
title: 'Quiz Show',
released: ISODate('1994-10-07T00:00:00.000Z'),
previousRuntime: 103,
runtimeComparison: 'longer'
},
{
rated: 'PG-13',
title: 'Hoop Dreams',
released: ISODate('1994-10-14T00:00:00.000Z'),
runtime: 170,
previousRuntime: 133,
runtimeComparison: 'longer'
},
{
runtime: 195,
rated: 'R',
title: "Schindler's List",
released: ISODate('1994-02-04T00:00:00.000Z'),
previousRuntime: null,
runtimeComparison: 'longer'
},
{
runtime: 121,
rated: 'R',
title: 'The Piano',
released: ISODate('1994-02-11T00:00:00.000Z'),
previousRuntime: 195,
runtimeComparison: 'shorter'
},
{
rated: 'R',
title: 'Pulp Fiction',
released: ISODate('1994-10-14T00:00:00.000Z'),
runtime: 154,
previousRuntime: 121,
runtimeComparison: 'longer'
},
{
runtime: 119,
title: 'Crumb',
released: ISODate('1995-04-28T00:00:00.000Z'),
rated: 'R',
previousRuntime: 154,
runtimeComparison: 'shorter'
},
{
rated: 'R',
title: 'To Die For',
released: ISODate('1995-10-06T00:00:00.000Z'),
runtime: 106,
previousRuntime: 119,
runtimeComparison: 'shorter'
}
]

この例では、次のことが行われます。

  • partitionBy: "$rated" はコレクション内のドキュメントをrated で分割し、各評価の計算は個別に保持されます。

  • sortBy: { released: 1 } sorts the documents in each partition by released in ascending order (1).

  • output.previousRuntime $shiftは、runtime 演算子を使用して、同じ評価内の直前の映画から を取得します。

  • $set は runtimeComparison という新しいフィールドを追加し、previousRuntime との比較に基づいてそのフィールドの値を longer、shorter、または same に設定します。

Tip

IoT 電力消費に関する追加の例については、電子書籍『 MongoDB の実用的な集計 』を参照してください。