对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。

$setWindowFields(聚合阶段)

$setWindowFields

版本 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

指定用于对文档分组的表达式。在 $setWindowFields 阶段,文档组被称为分区。默认为整个集合的一个分区。

对于某些操作符是必需的(参见限制条件)

指定分区中用于对文档进行排序的字段。使用与 $sort 阶段相同的语法。默认为不排序。

必需

在 $setWindowFields 阶段返回的输出中指定要附加到文档的字段。每个字段都被设置为窗口操作符返回的结果。

字段可以包含点,指定嵌入式文档字段和数组字段。$setWindowFields 阶段中嵌入式文档点状符号的语义与 $addFields 和 $set 阶段相同。请参阅嵌入式文档 $addFields 示例和嵌入式文档 $set 示例。

Optional

指定窗口边界和参数。窗口边界是包含性的。默认为无边界窗口,包括分区中的所有文档。

指定文档或范围窗口。

Optional

一个窗口,其中的上下界是相对于从集合中读取的当前文档的位置指定的。

窗口边界是使用包含下限和上限字符串或整数的双元素数组指定的。使用:

  • 输出中当前文档位置的 "current" 字符串。

  • 分区中第一个或最后一个文档位置的 "unbounded" 字符串。

  • 相对于当前文档的位置,可以用整数表示。对于当前文档之前的位置,可以使用负整数。对于当前文档之后的位置,可以使用正整数。0 是当前文档位置。

请参阅文档窗口示例。

Optional

在该窗口中,使用基于当前文档中 SortBy 字段值的范围来定义下边界和上表迎接。

窗口边界是使用包含下限和上限字符串或数字的双元素数组指定的。使用:

  • 输出中当前文档位置的 "current" 字符串。

  • 分区中第一个或最后一个文档位置的 "unbounded" 字符串。

  • 当前文档SortBy 字段值中要添加的数字。如果 sortBy 字段值包含在下限和上限边界内,则文档位于窗口中。

请参阅范围窗口示例。

Optional

指定时间范围窗口边界的单位。可以设置为以下字符串之一:

  • "year"

  • "quarter"

  • "month"

  • "week"

  • "day"

  • "hour"

  • "minute"

  • "second"

  • "millisecond"

如果省略,则使用默认的数值范围窗口边界。

请参阅时间范围窗口示例。

提示

示例

$setWindowFields 阶段会向现有文档添加新字段。可以在聚合操作中包含一个或多个 $setWindowFields 阶段。

从 MongoDB 5.3 开始,可以将 $setWindowFields 阶段与事务和 "snapshot" 读关注结合使用。

$setWindowFields 阶段不保证返回文档的顺序。

这些操作符可与 $setWindowFields 阶段一起使用:

$setWindowFields 阶段的限制:

  • 在 MongoDB 5.3 之前,无法使用 $setWindowFields 阶段:

  • 对于以下情况,sortBy 是必需的:

  • 范围窗口要求所有 sortBy 值均为数字。

  • 时间范围窗口要求所有 sortBy 值均为日期。

  • 范围和时间范围窗口只能包含一个 sortBy字段,并且排序必须为升序。

  • 无法同时指定文档窗口和范围窗口。

  • 这些操作符使用隐式窗口,如果您指定了窗口选项,它们会返回错误:

  • 对于范围窗口,窗口中只包含指定范围内的数字。不包括缺失值、未定义值和 null 值。

  • 对于时间范围窗口:

    • 窗口中只包含日期和时间类型。

    • 数值边界值必须为整数。例如,您可以使用 2 小时作为边界,但不能使用 1.5 小时。

  • 对于空窗口或具有不兼容值的窗口(例如,对字符串使用 $sum),返回值取决于操作符:

    • 对于$count和$sum,返回值为0 。

    • $addToSet 和 $push 的返回值为空数组。

    • 对于所有其他运算符,返回值为 null。

本页上的示例使用来自 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 }]

在示例中:

  • partitionBy: "$rated" 按 rated 对集合中的文档进行分区。分区为 G、PG、PG-13 和 R。

  • sortBy: { released: 1 } 按 released 升序 (1) 对每个分区中的文档进行排序,因此最早的 released 日期位于最前面。

  • output:

    • 将 cumulativeRuntimeForRating字段设置为每个 rated 值的累积 runtime,该值通过对分区中前一个值的连续添加而增加。

    • 使用在文档窗口中运行的 $sum 操作符计算累积 runtime。

      该窗口包含介于 unbounded 下限和 current 文档之间的文档。这意味着 $sum 会返回分区开始和当前文档之间的文档的累积 runtime。

此示例使用 $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 }]

在示例中:

  • partitionBy: { $year: "$released" } 按 中的$year 对集合中的文档进行 分区released 。分别有 1993、1994 和 1995 的分区。

  • sortBy: { released: 1 } 按 released 升序 (1) 对每个分区中的文档进行排序,因此最早的 released 日期位于最前面。

  • output:

    • 将 cumulativeRuntimeForYear 字段设置为每年的累积 runtime,这个值通过对分区中先前值的连续添加而增加。

    • 使用在文档窗口中运行的 $sum 操作符计算累积 runtime。

      该窗口包含介于 unbounded 下限和 current 文档之间的文档。这意味着 $sum 会返回分区开始和当前文档之间的文档的累积 runtime。

此示例使用$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" } 按 中的$year 对集合中的文档进行 分区released 。分别有 1993、1994 和 1995 的分区。

  • sortBy: { released: 1 } 按 released 升序 (1) 对每个分区中的文档进行排序,因此最早的 released 日期位于最前面。

  • output:

    • 将 averageRuntime 字段设置为每年的移动平均值 runtime。

    • 使用在文档窗口运行的 $avg 操作符计算移动平均值 runtime。

      窗口包含 -1 和 0 之间的文档。这意味着 $avg 会返回分区中当前文档 (0) 与前一个文档 (-1) 之间的移动平均 runtime。

此示例使用 $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 }]

在示例中:

  • partitionBy: { $year: "$released" } 按 中的$year 对集合中的文档进行 分区released 。分别有 1993、1994 和 1995 的分区。

  • sortBy: { released: 1 } 按 released 升序 (1) 对每个分区中的文档进行排序,因此最早的 released 日期位于最前面。

  • output:

    • 将cumulativeRuntimeForYear 字段设置为每年的累积runtime 。

    • 使用在文档窗口中运行的 $sum 操作符计算累积 runtime。

      该窗口包含介于 unbounded 下限和 current 文档之间的文档。这意味着 $sum 会返回分区开始和当前文档之间的文档的累积 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 }]

在示例中:

  • partitionBy: "$rated" 按 rated 对集合中的文档进行分区。分区为 G、PG、PG-13 和 R。

  • sortBy: { runtime: 1 } 按 runtime 以升序 (1) 对每个分区中的文档进行排序,因此最短的 runtime 位于最前面。

  • output 将 runtimeFromSimilarMovies 字段设置为范围窗口中文档的 runtime 值的总和。

    • 窗口包含介于 -10 下限和 10 上限之间的文档。该范围含边界值。

    • $sum 返回当前文档的 runtime 值的正负 10 分钟范围内包含的 runtime 值的总和。

以下示例使用的窗口的正上边界时间范围单位为$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') ] }]

在示例中:

  • partitionBy: "$rated" 按 rated 对集合中的文档进行分区。分区为 G、PG、PG-13 和 R。

  • sortBy: { released: 1 } 按 released 升序 (1) 对每个分区中的文档进行排序,因此最早的 released 日期位于最前面。

  • output:

    • 将 recentReleases字段设置为每个 rated 分区中文档的 released 值的大量。大量元素通过添加大量中的前一个元素进行扩展。

    • 使用 $push 从范围窗口中的文档返回 released 值的数组。

  • 该窗口包含在 unbounded 下限和使用时间范围单位将上限设置为 10(当前文档的 released 值之后 10 个月)之间的文档。

  • $push 返回分区起始和满足如下条件的文档之间文档的 released 值的数组:released 值在当前文档的 released 值(含)加上 10 10 个月范围内。

以下示例使用的窗口的上边界时间范围单位为$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') ] }]

在示例中:

  • partitionBy: "$rated" 按 rated 对集合中的文档进行分区。分区为 G、PG、PG-13 和 R。

  • sortBy: { released: 1 } 按 released 升序 (1) 对每个分区中的文档进行排序,因此最早的 released 日期位于最前面。

  • output:

    • 将 recentReleases字段设置为每个 rated 分区中文档的 released 值的大量。大量元素通过添加大量中的前一个元素进行扩展。

    • 使用 $push 从范围窗口中的文档返回 released 值的数组。

  • 该窗口包含在 unbounded 下限和使用时间范围单位将上限设置为 -10(当前文档的 released 值之前 10 个月)之间的文档。

  • $push 返回分区起始和满足如下条件的文档之间文档的 released 值的数组:released 值在当前文档的 released 值(含)减去 10 10 个月范围内。

以下示例使用 $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' }]

在示例中:

提示

有关物联网功耗的其他示例,请参阅实用 MongoDB 聚合电子书。