定義
バージョン 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 | |
Required for some operators (see Restrictions) | パーティション内のドキュメントを並べ替えるフィールドを指定します。 | |
必須 | Specifies the field(s) to append to the documents in the output returned by the A field can contain dots to specify embedded document fields and array fields. The semantics for the embedded document dotted notation in the
| |
任意 | ||
任意 | コレクションから読み取られた現在のドキュメントの位置を基準にして下限と上限が指定されるウィンドウ。 ウィンドウの境界は、下限と上限の文字列または整数を含む 2 つの要素の配列を使用して指定されます。次を使用します。
「ドキュメント ウィンドウの例」を参照してください。 | |
任意 | ||
任意 | 時間の範囲ウィンドウ境界の単位を指定します。次のいずれかの文字列に設定できます。
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:
- ギャップ補充演算子:
$linearFillおよび$locf。
ランク演算子:
$denseRank、$documentNumber、および$rank。範囲演算子:
$minMaxScaler
Explain の出力
バージョン8.3の新機能。
$setWindowsFields操作で explain を実行すると、出力には、使用中の追跡メモリの最大バイト数を含む peakTrackedMemBytesフィールドが含まれます。詳しくは、explain.executionStats.peakTrackedMemBytes を参照してください。
制限事項
Restrictions for the $setWindowFields stage:
Before MongoDB 5.3, the
$setWindowFieldsstage cannot be used:トランザクション内。
"snapshot"読み取り保証(read concern)内。
For range windows, only numbers in the specified range are included in the window. Missing, undefined, and
nullvalues are excluded.For time range windows:
ウィンドウには日付と時刻の種類のみが含まれます。
数値境界値は整数である必要があります。たとえば、境界として 2 時間を使用できますが、1.5 時間は使用できません。
例
このページの例では、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 } ]
この例では、次のことが行われます。
output:cumulativeRuntimeForRatingフィールドを各rated値の累積runtimeに設定します。これは、パーティション内の前の値に連続して追加されることによって増加します。Calculates the cumulative
runtimeusing the$sumoperator run in a documents window.The window contains documents between an
unboundedlower limit and thecurrentdocument. This means$sumreturns the cumulativeruntimefor 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$yearinreleased. There are partitions for1993,1994, and1995.output:cumulativeRuntimeForYearフィールドを各年の累積runtimeに設定します。これは、パーティション内の前の値に連続して追加されることによって増加します。Calculates the cumulative
runtimeusing the$sumoperator run in a documents window.The window contains documents between an
unboundedlower limit and thecurrentdocument. This means$sumreturns the cumulativeruntimefor 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$yearinreleased. There are partitions for1993,1994, and1995.output:averageRuntimeフィールドを各年の移動平均runtimeに設定します。
ドキュメント ウィンドウを使用して各年の累計実行時間と最大実行時間を取得します
この例では、 $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$yearinreleased. There are partitions for1993,1994, and1995.output:cumulativeRuntimeForYearフィールドを各年の累積runtimeに設定します。Calculates the cumulative
runtimeusing the$sumoperator run in a documents window.The window contains documents between an
unboundedlower limit and thecurrentdocument. This means$sumreturns the cumulativeruntimefor the documents between the beginning of the partition and the current document.maximumRuntimeForYearフィールドを各年の最大値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 } ]
この例では、次のことが行われます。
時間範囲ウィンドウの例
上限が正の値である時間範囲ウィンドウを使用する
次の例では、 $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') ] } ]
この例では、次のことが行われます。
$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') ] } ]
この例では、次のことが行われます。
$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' } ]
この例では、次のことが行われます。
次の WeatherMeasurementクラスは、気象測定値のコレクション内のドキュメントを表します。
[] public class WeatherMeasurement { [] public ObjectId Id { get; set; } [] public string LocalityId { get; set; } = null!; [] public DateTime MeasurementDateTime { get; set; } [] public float Rainfall { get; set; } [] public float Temperature { get; set; } }
MongoDB .NET/ C#ドライバーを使用して$setWindowFields ステージを集計パイプラインに追加するには、 PipelineDefinitionオブジェクトで UnionWith() メソッドを呼び出します。
次の例では、Rainfall フィールドと Temperature フィールドを使用して、各地域の過去 1 か月の累積降量、移動平均温度、中央値、90 パーセンタイル降量を計算するパイプラインステージを作成します。
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) ), MedianTemperature = o.Median( w => w.Temperature, RangeWindow.Create( RangeWindow.Months(-1), RangeWindow.Current) ), NinetiethPercentileRainfall = o.Percentile( w => w.Rainfall, new[] { 0.9 }, RangeWindow.Create( RangeWindow.Months(-1), RangeWindow.Current) ) } );
このページの Node.js の例では、Atlas サンプルデータセットの sample_weatherdata.data コレクションを使用します。無料の MongoDB Atlas クラスターを作成し、サンプルデータセットをロードする方法を学ぶには、MongoDB Node.js ドライバーのドキュメント「使用開始」をご覧ください。
MongoDB Node.jsドライバーを使用して $setWindowFields ステージを集計パイプラインに追加するには、パイプラインオブジェクトで $setWindowFields 演算子を使用します。
次の例では、過去1か月間における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;
Tip
IoT 電力消費に関する追加の例については、電子書籍『 MongoDB の実用的な集計 』を参照してください。