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

$bottomN(累加器操作符)

$bottomN

5.2版本新增。

根据指定的排序顺序,返回群组内后 n 个元素的聚合。 如果该组包含的元素少于 n 个,$bottomN 返回该组中的所有元素。

注意

消歧

本页介绍了 $bottomN 用作 累加器 时的情况。累加器返回跨一组输入文档的聚合值。

您还可以在这些其他上下文中使用 bottomN:

{
$bottomN:
{
n: <expression>,
output: <expression>,
sortBy: { <field1>: <sort order>, <field2>: <sort order> ... }
}
}
字段
必要性
说明

n

必需

The number of elements that $bottomN returns from the group. For an example of setting n dynamically, see Set n Dynamically Based on the Group Key.

output

必需

确定组中每个元素的输出。可以是任何表达式。

sortBy

必需

定义每个组中元素的排序顺序,语法类似于 $sort。不影响管道输出中文档的顺序。

  • $bottomN 不筛选空值。

  • $bottomN 将缺失值转换为 null 并保留在输出中。

db.aggregate( [
{
$documents: [
{ playerId: "PlayerA", gameId: "G1", score: 1 },
{ playerId: "PlayerB", gameId: "G1", score: 2 },
{ playerId: "PlayerC", gameId: "G1", score: 3 },
{ playerId: "PlayerD", gameId: "G1"},
{ playerId: "PlayerE", gameId: "G1", score: null }
]
},
{
$group:
{
_id: "$gameId",
playerId:
{
$bottomN:
{
output: [ "$playerId", "$score" ],
sortBy: { "score": -1 },
n: 3
}
}
}
}
] )

在本例中:

  • $documents 创建包含玩家分数的字面文档。

  • $group 按 gameId 对文档进行分组。此示例只有一个 gameId、G1。

  • PlayerD 分数缺失,且 PlayerE 的 score 为空。这些值都被视为空值。

  • playerId 和 score 字段指定为 output : ["$playerId"," $score"],并以数组值形式返回。

  • 由于sortBy: { "score" : -1 } ,空值被排序到返回的playerId数组的末尾。

[
{
_id: "G1",
playerId: [ [ "PlayerA", 1 ], [ "PlayerD", null ], [ "PlayerE", null ] ]
}
]

对不同类型进行排序时,将使用BSON 数据类型的顺序来确定顺序。 例如,考虑一个其值由字符串和数字组成的collection。

  • 在升序排序中,字符串值排在数值之后。

  • 在降序排序中,字符串值排在数值之前。

db.aggregate( [
{
$documents: [
{ playerId: "PlayerA", gameId: "G1", score: 1 },
{ playerId: "PlayerB", gameId: "G1", score: "2" },
{ playerId: "PlayerC", gameId: "G1", score: "" }
]
},
{
$group:
{
_id: "$gameId",
playerId: {
$bottomN:
{
output: ["$playerId","$score"],
sortBy: {"score": -1},
n: 3
}
}
}
}
] )

在本例中:

  • PlayerA 有一个整数分数。

  • PlayerB 有一个字符串"2"分数。

  • PlayerC 有一个空字符串分数。

由于按降序{ "score" : -1 }排序,因此字符串字面值会排序在PlayerA的数值之前:

[
{
_id: "G1",
playerId: [ [ "PlayerB", "2" ], [ "PlayerC", "" ], [ "PlayerA", 1 ] ]
}
]

本页上的示例使用来自 sample_mflix 数据集的数据。有关如何将此数据集加载到您的自管理 MongoDB 部署中的详细信息,请参阅 加载示例数据集。如果您对示例数据库进行了任何修改,您可能需要删除并重新创建数据库,才能运行此页面上的示例。

您可以使用 $bottomN 累加器查找 Short 类型中评分最低的三部电影。

db.movies.aggregate( [
{
$match: {
genres: "Short",
"imdb.rating": { $exists: true }
}
},
{
$group: {
_id: "Short",
lowestRatedMovies: {
$bottomN: {
output: [ "$title", "$imdb.rating" ],
sortBy: { "imdb.rating": -1, title: 1 },
n: 3
}
}
}
}
] )

示例管道:

  • 使用 $match 将输入限制为包含 imdb.rating 值的 Short 部电影。

  • 使用 $group 将匹配的电影放入单个 Short 组。

  • 使用 sortBy: { "imdb.rating": -1, title: 1 } 按评级对电影进行排序,并使用 title 作为平局破坏程序。

  • 指定由 $bottomN 使用 output: [ "$title", "$imdb.rating" ] 返回的字段。

  • 使用 $bottomN 返回包含 n: 3 的三个最低匹配电影。

您可以使用 $bottomN 累加器来查找每个类型中评分最低的电影。

db.movies.aggregate( [
{
$unwind: "$genres"
},
{
$match: {
"imdb.rating": { $exists: true }
}
},
{
$group: {
_id: "$genres",
lowestRatedMovie: {
$bottomN: {
output: [ "$title", "$imdb.rating" ],
sortBy: { "imdb.rating": -1, title: 1 },
n: 3
}
}
}
},
{
$sort: { _id: 1 }
},
{
$limit: 5
}
] )

示例管道:

  • 使用 $unwind 扩展每个电影的 genres 数组。

  • 使用 $match 以保留仅包含 imdb.rating 值的文档。

  • 使用 $group 按类型对结果分组。

  • 指定由 $bottomN 使用 output: [ "$title", "$imdb.rating" ] 返回的字段。

  • 使用 sortBy: { "imdb.rating": -1, title: 1 } 按评级对电影进行排名,并使用 title 作为确定性的平局打破者。

  • 使用 $bottomN 返回每种类型中排名最后的三部电影,并且包含 n: 3。

  • 使用 $sort 和 $limit 按字母顺序显示前五个类别。

您可以动态地赋值 n 的值。以下示例使用 $cond 表达式,根据内容评级返回不同数量的电影。

db.movies.aggregate([
{
$match: {
rated: { $in: [ "G", "PG", "PG-13", "R" ] },
"imdb.rating": { $exists: true }
}
},
{
$group:
{
_id: { rated: "$rated" },
movies:
{
$bottomN:
{
output: { title: "$title", rating: "$imdb.rating" },
n: { $cond: { if: { $eq: [ "$rated", "PG" ] }, then: 3, else: 1 } },
sortBy: { "imdb.rating": -1 }
}
}
}
},
{
$sort: { "_id.rated": 1 }
}
] )

示例管道:

  • 使用 $match 以保留只包含 G、PG、PG-13 或 R 内容分级且包含 imdb.rating 值的电影。

  • 使用 $group 按内容评级对结果进行分组,并使用 _id: { rated: "$rated" } 。

  • 指定由 $bottomN 使用 output: { title: "$title", rating: "$imdb.rating" } 返回的字段。

  • 如果内容评级为 PG,则 n 为 3。否则,n 是 1。

  • 使用 sortBy: { "imdb.rating": -1 } 找出按 IMDb 评分排名最低的 n 部电影。

  • 按内容评级字母顺序排列结果。

给本页内容打分