MongoDB 聚合排序
要使用 MongoDB 进行聚合操作,需要对聚合管道阶段有足够的了解。本教程将通过一个示例来探索 $sort
阶段以执行 MongoDB 聚合排序。
MongoDB 聚合排序
让我们有一个场景来练习代码示例。我们有一个名为 student
的集合,其中包含基本信息。
你还可以使用以下查询与我们在同一页面上。
示例代码:
> db.createCollection('student');
> db.student.insertMany([
{"student_code": "ma001", "name": "Mehvish Ashiq", "postcode": 2000},
{"student_code": "tc002", "name": "Thomas Christopher", "postcode": 2001},
{"student_code": "km003", "name": "Krishna Mehta", "postcode": 2000},
{"student_code": "ar004", "name": "Aftab Raza", "postcode": 2000},
{"student_code": "za005", "name": "Zeenat Ashraf", "postcode": 2002},
{"student_code": "ka006", "name": "Kanwal Ali", "postcode": 2002}
]);
> db.student.find().pretty();
输出:
{
"_id" : ObjectId("629afe5a0a3b0e96fe92e97b"),
"student_code" : "ma001",
"name" : "Mehvish Ashiq",
"postcode" : 2000
}
{
"_id" : ObjectId("629afe5a0a3b0e96fe92e97c"),
"student_code" : "tc002",
"name" : "Thomas Christopher",
"postcode" : 2001
}
{
"_id" : ObjectId("629afe5a0a3b0e96fe92e97d"),
"student_code" : "km003",
"name" : "Krishna Mehta",
"postcode" : 2000
}
{
"_id" : ObjectId("629afe5a0a3b0e96fe92e97e"),
"student_code" : "ar004",
"name" : "Aftab Raza",
"postcode" : 2000
}
{
"_id" : ObjectId("629afe5a0a3b0e96fe92e97f"),
"student_code" : "za005",
"name" : "Zeenat Ashraf",
"postcode" : 2002
}
{
"_id" : ObjectId("629afe5a0a3b0e96fe92e980"),
"student_code" : "ka006",
"name" : "Kanwal Ali",
"postcode" : 2002
}
接下来,我们要确定容纳最多学生但以排序形式存在的 postcode
。为此,请检查以下示例代码。
示例代码:
> db.student.aggregate([
{ $group: { _id: '$postcode', students: { $sum: 1 } } },
{ $sort: {_id: 1} }
]);
输出:
{ "_id" : 2000, "students" : 3 }
{ "_id" : 2001, "students" : 1 }
{ "_id" : 2002, "students" : 2 }
在这里,我们使用 aggregate()
函数计算指定集合或视图中数据的聚合值。在 aggregate()
方法中,我们根据 postcode
字段对文档进行分组。
此外,我们将 postcode
返回为 _id
,学生将每个 postcode
计为 students
。在这里,我们使用 $sum
来获取每个 postcode
的学生人数。
$sum
通过忽略非数值来计算并返回数值的总和。
最后,我们使用 $sort
阶段根据项目需要以升序或降序对它们进行排序。1
用于升序,而 -1
用于降序。
相关文章
在 MongoDB Shell 中列出所有数据库
发布时间:2023/05/11 浏览次数:180 分类:MongoDB
-
交互式 Mongo Shell 提供了多个用于获取数据的选项。 本文介绍了在 Mongo Shell 中列出数据库的几种不同方法。
MongoDB 中检查字段包含的字符串
发布时间:2023/05/11 浏览次数:1024 分类:MongoDB
-
这篇文章解决了如何在 MongoDB 中使用正则表达式来确定字段是否包含字符串。在 MongoDB 中使用正则表达式 正则表达式 (regex) 是定义搜索模式的文本字符串。
在 MongoDB 中 upsert 更新插入
发布时间:2023/05/11 浏览次数:214 分类:MongoDB
-
在 MongoDB 中,upsert 结合了更新和插入命令。 它可以在 update() 和 findAndModify() 操作中使用。MongoDB 中的 upsert 查询 upsert 采用单个布尔参数。
如何卸载 MongoDB
发布时间:2023/05/11 浏览次数:745 分类:MongoDB
-
要从您的计算机中卸载 MongoDB,您必须先删除 MongoDB 服务、数据库和日志文件。使用这篇 MongoDB 文章,您将能够从 Ubuntu Linux、Mac 和 Windows 卸载 MongoDB。 请务必保留数据备份,因为一旦卸载,便
在 MongoDB 中存储日期和时间
发布时间:2023/05/11 浏览次数:762 分类:MongoDB
-
本 MongoDB 教程解释了 Date() 对象是什么以及如何使用 Date() 方法对集合进行排序。 这也将帮助您找到在 MongoDB 中显示和存储日期/时间的最佳方法。
MongoDB 按 ID 查找
发布时间:2023/05/11 浏览次数:1856 分类:MongoDB
-
MongoDB 中的 find by Id() 函数用于获取与用户提供的 id 相匹配的文档。 如果找不到与指定 ID 匹配的文档,则返回空值。
检查 MongoDB 服务器是否正在运行
发布时间:2023/05/11 浏览次数:247 分类:MongoDB
-
这篇 MongoDB 教程将告诉您如何检查是否安装了 MongoDB 以及安装的 MongoDB 服务器的版本。 它在 Windows、UBUNTU 和 MAC 等不同的操作系统中实现。
MongoDB 中的分页
发布时间:2023/05/11 浏览次数:174 分类:MongoDB
-
这篇文章将介绍什么是 MongoDB 中的分页。 为什么在 MongoDB 中需要分页以及在 MongoDB 中完成分页的不同方法或方式是什么。
MongoDB 从查询开始
发布时间:2023/05/11 浏览次数:186 分类:MongoDB
-
在这篇 MongoDB 文章中,用户将学习如何使用 $regex 进行开始查询。 它为查询中的模式匹配字符串提供正则表达式功能。