How to get the last N records in MongoDB?


To get the last N records in MongoDB, you need to use limit(). The syntax is as follows:

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

To understand the above syntax, let us create a collection with document. The query to create a collection with document is as follows:

> 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")
}

Display all documents from a collection with the help of find() method. The query is as follows:

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

The following is the output:

{ "_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" }

Here is the query to get last N records from a collection with the help of $natural and limit():

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

The following is the output:

{ "_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" }

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements