获取 MongoDB 中的最后 N 条记录
本文将探讨获取 MongoDB 中最后 N 条记录的方法数量,其中 N 为正数且大于零。我们将看到如何检索有无排序的文档数量。
获取 MongoDB 中的最后 N 条记录
我们必须有一个包含文档的集合才能继续实际示例。因此,让我们继续使用以下查询来创建一个集合并在其中插入一些文档。
示例代码:
> use get_n_records;
> db.createCollection('stock');
> db.stock.insertMany([
{"item": "abc", "quantity": 12},
{"item": "def", "quantity": 234},
{"item": "ghi", "quantity": 45},
{"item": "jkl", "quantity": 345},
{"item": "mno", "quantity": 243},
{"item": "pqr", "quantity": 345},
{"item": "stu", "quantity": 56},
{"item": "vwx", "quantity": 575},
{"item": "yzz", "quantity": 398}
]);
> db.stock.find();
输出:
{ "_id" : ObjectId("629ac651b7e637e64d8e75fb"), "item" : "abc", "quantity" : 12 }
{ "_id" : ObjectId("629ac651b7e637e64d8e75fc"), "item" : "def", "quantity" : 234 }
{ "_id" : ObjectId("629ac651b7e637e64d8e75fd"), "item" : "ghi", "quantity" : 45 }
{ "_id" : ObjectId("629ac651b7e637e64d8e75fe"), "item" : "jkl", "quantity" : 345 }
{ "_id" : ObjectId("629ac651b7e637e64d8e75ff"), "item" : "mno", "quantity" : 243 }
{ "_id" : ObjectId("629ac651b7e637e64d8e7600"), "item" : "pqr", "quantity" : 345 }
{ "_id" : ObjectId("629ac651b7e637e64d8e7601"), "item" : "stu", "quantity" : 56 }
{ "_id" : ObjectId("629ac651b7e637e64d8e7602"), "item" : "vwx", "quantity" : 575 }
{ "_id" : ObjectId("629ac651b7e637e64d8e7603"), "item" : "yzz", "quantity" : 398 }
现在,我们已经准备好并填充了我们的集合。关键是要考虑我们是否要从最近插入的文档到最近插入的文档中获取最后 N 条记录,反之亦然。
让我们学习下面给出的每个场景。
从最近插入的文档到最近插入的文档中获取最后 N 条记录,反之亦然
在继续之前,请查看以下两个术语以轻松理解它们:
使用 cursor.sort()
方法从最近插入的文档到最近插入的文档中获取最后 N 条记录,反之亦然
示例代码(后进先出):
> db.stock.find().sort({_id:-1}).limit(3);
输出:
{ "_id" : ObjectId("629ac651b7e637e64d8e7603"), "item" : "yzz", "quantity" : 398 }
{ "_id" : ObjectId("629ac651b7e637e64d8e7602"), "item" : "vwx", "quantity" : 575 }
{ "_id" : ObjectId("629ac651b7e637e64d8e7601"), "item" : "stu", "quantity" : 56 }
示例代码(先进先出):
> db.stock.find().sort({_id:1}).limit(3);
输出:
{ "_id" : ObjectId("629ac651b7e637e64d8e75fb"), "item" : "abc", "quantity" : 12 }
{ "_id" : ObjectId("629ac651b7e637e64d8e75fc"), "item" : "def", "quantity" : 234 }
{ "_id" : ObjectId("629ac651b7e637e64d8e75fd"), "item" : "ghi", "quantity" : 45 }
在这里,我们使用 sort()
方法将文档从最近到最近的插入顺序(降序)排序。因此,在从数据库中获取任何文档(记录)之前,我们将 sort()
方法应用于游标。
sort()
方法的字段值对为 {field: value}
,我们最多可以对 32 个键执行排序。使用 MongoDB 时,文档未按指定顺序存储在集合中。
这就是为什么当我们对具有重复值的特定字段执行排序时,具有这些值的文档可能会以任何顺序返回。为了获得一致的排序,我们必须至少包含一个包含唯一值的字段。
确保这一点的最简单方法是在 sort()
方法中使用 _id
字段。
limit(N)
用于获取指定数量的文档。如果我们有兴趣获取所有排序的文档,我们可以省略这个函数。
或者,我们也可以在没有特定字段的情况下使用如下查询。
示例代码:
> db.stock.find().sort({$natural:1}).limit(3);
输出:
{ "_id" : ObjectId("629ac651b7e637e64d8e75fb"), "item" : "abc", "quantity" : 12 }
{ "_id" : ObjectId("629ac651b7e637e64d8e75fc"), "item" : "def", "quantity" : 234 }
{ "_id" : ObjectId("629ac651b7e637e64d8e75fd"), "item" : "ghi", "quantity" : 45 }
如果 $natural
的值分别为 1
和 -1
,则它们的工作方式类似于先进先出和后进先出。如果我们有 MongoDB 4.4 或更高版本,我们可以使用它。
使用 $sort
和 $limit
(聚合) 阶段从最近插入的文档到最近插入的文档中获取最后 N 条记录,反之亦然
示例代码(后进先出):
> db.stock.aggregate([
{ $sort : { _id : -1 } },
{ $limit : 4 }
]);
输出:
{ "_id" : ObjectId("629ac651b7e637e64d8e7603"), "item" : "yzz", "quantity" : 398 }
{ "_id" : ObjectId("629ac651b7e637e64d8e7602"), "item" : "vwx", "quantity" : 575 }
{ "_id" : ObjectId("629ac651b7e637e64d8e7601"), "item" : "stu", "quantity" : 56 }
{ "_id" : ObjectId("629ac651b7e637e64d8e7600"), "item" : "pqr", "quantity" : 345 }
示例代码(先进先出):
> db.stock.aggregate([
{ $sort : { _id : 1 } },
{ $limit : 4 }
]);
输出:
{ "_id" : ObjectId("629ac651b7e637e64d8e75fb"), "item" : "abc", "quantity" : 12 }
{ "_id" : ObjectId("629ac651b7e637e64d8e75fc"), "item" : "def", "quantity" : 234 }
{ "_id" : ObjectId("629ac651b7e637e64d8e75fd"), "item" : "ghi", "quantity" : 45 }
{ "_id" : ObjectId("629ac651b7e637e64d8e75fe"), "item" : "jkl", "quantity" : 345 }
$sort
聚合阶段类似于 cursor.sort()
方法。它用于对所有文档进行排序,并将它们按排序顺序返回到管道。
它需要一个具有字段名称和相应排序顺序的文档。请记住,1
和 -1
用于升序和降序。
与 cursor.sort()
方法一样,我们可以使用 $sort
聚合阶段对最多 32 个键进行排序,并具有至少一个字段(包含唯一值)以实现一致的排序顺序。
如果我们需要对多个字段进行排序,排序顺序是从左到右评估的。而 $limit
采用一个数字,显示需要在屏幕上打印多少个文档;此外,它具有 64 位整数限制。
结合使用 skip()
和 count()
方法获取 MongoDB 中的最后 N 条记录
这种方法很吸引人,因为考虑到项目要求,我们不是对数据进行排序,而是从头或尾获取一段文档。
示例代码(获取前 3 条记录而不对文档进行排序):
> db.stock.find().limit(3);
输出:
{ "_id" : ObjectId("629ac651b7e637e64d8e75fb"), "item" : "abc", "quantity" : 12 }
{ "_id" : ObjectId("629ac651b7e637e64d8e75fc"), "item" : "def", "quantity" : 234 }
{ "_id" : ObjectId("629ac651b7e637e64d8e75fd"), "item" : "ghi", "quantity" : 45 }
示例代码(获取最后 3 条记录而不对文档进行排序):
> db.stock.find().skip(db.stock.count() - 3)
输出:
{ "_id" : ObjectId("629ac651b7e637e64d8e7601"), "item" : "stu", "quantity" : 56 }
{ "_id" : ObjectId("629ac651b7e637e64d8e7602"), "item" : "vwx", "quantity" : 575 }
{ "_id" : ObjectId("629ac651b7e637e64d8e7603"), "item" : "yzz", "quantity" : 398 }
在这个例子中,我们使用 db.stock.count()
来计算文档的总数,从中减去 3 并使用 skip()
方法跳过所有文档。
请记住,我们减去的文档数量没有被跳过(在本例中为 3)。我们将得到这 3 个文件。
相关文章
在 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 进行开始查询。 它为查询中的模式匹配字符串提供正则表达式功能。