定义
版本 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 阶段采用包含以下字段的文档:
字段 | 必要性 | 说明 |
|---|---|---|
Optional | 指定用于对文档分组的表达式。在 | |
对于某些操作符是必需的(参见限制条件) | 指定分区中用于对文档进行排序的字段。使用与 | |
必需 | 在 字段可以包含点,指定嵌入式文档字段和数组字段。
| |
Optional | ||
Optional | 一个窗口,其中的上下界是相对于从集合中读取的当前文档的位置指定的。 窗口边界是使用包含下限和上限字符串或整数的双元素数组指定的。使用:
请参阅文档窗口示例。 | |
Optional | ||
Optional |
提示
行为
$setWindowFields 阶段会向现有文档添加新字段。可以在聚合操作中包含一个或多个 $setWindowFields 阶段。
从 MongoDB 5.3 开始,可以将 $setWindowFields 阶段与事务和 "snapshot" 读关注结合使用。
$setWindowFields 阶段不保证返回文档的顺序。
窗口运算符
这些操作符可与 $setWindowFields 阶段一起使用:
- Accumulator operators:
$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。
限制
$setWindowFields 阶段的限制:
在 MongoDB 5.3 之前,无法使用
$setWindowFields阶段:在事务内。
带有
"snapshot"读关注。
对于范围窗口,窗口中只包含指定范围内的数字。不包括缺失值、未定义值和
null值。对于时间范围窗口:
窗口中只包含日期和时间类型。
数值边界值必须为整数。例如,您可以使用 2 小时作为边界,但不能使用 1.5 小时。
示例
本页上的示例使用来自 sample_mflix 数据集的数据。有关如何将此数据集加载到您的自管理 MongoDB 部署中的详细信息,请参阅 加载示例数据集。如果您对示例数据库进行了任何修改,您可能需要删除并重新创建数据库,才能运行此页面上的示例。
文档窗口示例
使用文档窗口获取每个评级的累积运行时间
此示例使用$setWindowFields 格式的文档窗口来输出每个 rated 值的累积 runtime。在示例输出中,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 }]在示例中:
使用文档窗口获取每年的累计运行时间
此示例使用 $setWindowFields 中的文档窗口输出 released 中每个 $year 的累积 runtime。在示例输出中,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 }]在示例中:
使用文档窗口获取每年的移动平均运行时间
此示例使用$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 }]在示例中:
使用文档窗口获取每年的累计运行时间和最大运行时间
此示例使用 $setWindowFields 中的文档窗口输出 released 中每个 $year 的累积和最大 runtime 值。在示例输出中,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 }]在示例中:
output:将
cumulativeRuntimeForYear字段设置为每年的累积runtime。将
maximumRuntimeForYear字段设置为每年的最高量 (runtime)。使用在文档窗口中运行的
$max操作符,计算所有文档的最大runtime。该窗口包含介于
unbounded下限和upper限值之间的文档。这意味着$max返回分区中文档的最大值runtime。
范围窗口示例
此示例使用$setWindowFields 中的范围窗口,返回在当前文档的 runtime 值的正负 10 分钟内上映的电影的 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 }]在示例中:
时间范围窗口示例
使用具有正上界的时间范围窗口
以下示例使用的窗口的正上边界时间范围单位为$setWindowFields。管道为匹配指定时间范围的每个 rated 值输出一个 released 值大量。在示例输出中,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值的数组:released值在当前文档的released值(含)加上1010 个月范围内。
使用具有负上界的时间范围窗口
以下示例使用的窗口的上边界时间范围单位为$setWindowFields,单位为负。管道为匹配指定时间范围的每个 rated 值输出一个 released 值大量。在示例输出中,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值的数组:released值在当前文档的released值(含)减去1010 个月范围内。
与上一个值的比较示例
以下示例使用 $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' }]在示例中:
sortBy: { released: 1 }按released升序 (1) 对每个分区中的文档进行排序。output.previousRuntime使用$shift操作符捕获同一评级中前一部电影的runtime。$set添加一个名为runtimeComparison的新字段,并根据与previousRuntime的比较将该字段的值设置为longer、shorter或same。
以下 WeatherMeasurement 类表示天气测量集合中的文档:
[BsonIgnoreExtraElements]public class WeatherMeasurement{ [BsonId] public ObjectId Id { get; set; } [BsonElement("localityId")] public string LocalityId { get; set; } = null!; [BsonElement("measurementDateTime")] public DateTime MeasurementDateTime { get; set; } [BsonElement("rainfall")] public float Rainfall { get; set; } [BsonElement("temperature")] public float Temperature { get; set; }}要使用MongoDB .NET/C# 驱动程序将 $setWindowFields 阶段添加到聚合管道,请对 PipelineDefinition 对象调用 UnionWith() 方法。
以下示例创建了一个管道阶段,该阶段使用 Rainfall 和 Temperature 字段计算每个地区过去一个月的累计降雨量、移动平均温度、中位数温度和 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操作符。
以下示例创建了一个管道阶段,该阶段计算过去一个月内 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;提示
有关物联网功耗的其他示例,请参阅实用 MongoDB 聚合电子书。