如何在 MongoDB 中获取最后 N 条记录?


要在 MongoDB 中获取最后 N 条记录,你需要使用 limit() 函数。语法如下:

db.yourCollectionName.find().sort({$natural:-1}).limit(yourValue);

为了理解上述语法,让我们创建一个带有文档的集合。创建带有文档的集合的查询如下:

> db.getLastNRecordsDemo.insertOne({"EmployeeName":"Maxwell"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c6ecf3d6fd07954a4890689")
}
> db.getLastNRecordsDemo.insertOne({"EmployeeName":"Carol"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c6ecf496fd07954a489068a")
}
> db.getLastNRecordsDemo.insertOne({"EmployeeName":"Bob"});
{
"acknowledged" : true,
   "insertedId" : ObjectId("5c6ecf4e6fd07954a489068b")
}
> db.getLastNRecordsDemo.insertOne({"EmployeeName":"Sam"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c6ecf546fd07954a489068c")
}
> db.getLastNRecordsDemo.insertOne({"EmployeeName":"Robert"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c6ecf596fd07954a489068d")
}
> db.getLastNRecordsDemo.insertOne({"EmployeeName":"Mike"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c6ecf606fd07954a489068e")
}
> db.getLastNRecordsDemo.insertOne({"EmployeeName":"Chris"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c6ecf686fd07954a489068f")
}
> db.getLastNRecordsDemo.insertOne({"EmployeeName":"James"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c6ecf6f6fd07954a4890690")
}
> db.getLastNRecordsDemo.insertOne({"EmployeeName":"Jace"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c6ecf756fd07954a4890691")
}
> db.getLastNRecordsDemo.insertOne({"EmployeeName":"Ramit"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c6ecf7d6fd07954a4890692")
}
> db.getLastNRecordsDemo.insertOne({"EmployeeName":"David"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c6ecf8d6fd07954a4890693")
}

通过 find() 函数显示集合中的所有文档。查询如下:

> db.getLastNRecordsDemo.find().pretty();

输出如下:

{ "_id" : ObjectId("5c6ecf3d6fd07954a4890689"), "EmployeeName" : "Maxwell" }
{ "_id" : ObjectId("5c6ecf496fd07954a489068a"), "EmployeeName" : "Carol" }
{ "_id" : ObjectId("5c6ecf4e6fd07954a489068b"), "EmployeeName" : "Bob" }
{ "_id" : ObjectId("5c6ecf546fd07954a489068c"), "EmployeeName" : "Sam" }
{ "_id" : ObjectId("5c6ecf596fd07954a489068d"), "EmployeeName" : "Robert" }
{ "_id" : ObjectId("5c6ecf606fd07954a489068e"), "EmployeeName" : "Mike" }
{ "_id" : ObjectId("5c6ecf686fd07954a489068f"), "EmployeeName" : "Chris" }
{ "_id" : ObjectId("5c6ecf6f6fd07954a4890690"), "EmployeeName" : "James" }
{ "_id" : ObjectId("5c6ecf756fd07954a4890691"), "EmployeeName" : "Jace" }
{ "_id" : ObjectId("5c6ecf7d6fd07954a4890692"), "EmployeeName" : "Ramit" }
{ "_id" : ObjectId("5c6ecf8d6fd07954a4890693"), "EmployeeName" : "David" }

以下查询可以通过 $natural 和 limit() 函数从集合中获取最后 N 条记录

> db.getLastNRecordsDemo.find().sort({$natural:-1}).limit(7);

输出如下:

{ "_id" : ObjectId("5c6ecf8d6fd07954a4890693"), "EmployeeName" : "David" }
{ "_id" : ObjectId("5c6ecf7d6fd07954a4890692"), "EmployeeName" : "Ramit" }
{ "_id" : ObjectId("5c6ecf756fd07954a4890691"), "EmployeeName" : "Jace" }
{ "_id" : ObjectId("5c6ecf6f6fd07954a4890690"), "EmployeeName" : "James" }
{ "_id" : ObjectId("5c6ecf686fd07954a489068f"), "EmployeeName" : "Chris" }
{ "_id" : ObjectId("5c6ecf606fd07954a489068e"), "EmployeeName" : "Mike" }
{ "_id" : ObjectId("5c6ecf596fd07954a489068d"), "EmployeeName" : "Robert" }

更新于:30-7-2019

1K+ 浏览

启动你的 职业生涯

通过完成课程获得认证

开始
广告