Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to get the last N records in MongoDB?
To get the last N records in MongoDB, you need to use limit() method combined with sorting. The $natural parameter sorts documents in reverse insertion order, and limit() restricts the number of returned records.
Syntax
db.yourCollectionName.find().sort({$natural:-1}).limit(yourValue);
Sample Data
To understand the above syntax, let us create a collection with documents. The query to create a collection with documents is as follows ?
db.getLastNRecordsDemo.insertMany([
{"EmployeeName": "Maxwell"},
{"EmployeeName": "Carol"},
{"EmployeeName": "Bob"},
{"EmployeeName": "Sam"},
{"EmployeeName": "Robert"},
{"EmployeeName": "Mike"},
{"EmployeeName": "Chris"},
{"EmployeeName": "James"},
{"EmployeeName": "Jace"},
{"EmployeeName": "Ramit"},
{"EmployeeName": "David"}
]);
{
"acknowledged": true,
"insertedIds": [
ObjectId("5c6ecf3d6fd07954a4890689"),
ObjectId("5c6ecf496fd07954a489068a"),
ObjectId("5c6ecf4e6fd07954a489068b"),
ObjectId("5c6ecf546fd07954a489068c"),
ObjectId("5c6ecf596fd07954a489068d"),
ObjectId("5c6ecf606fd07954a489068e"),
ObjectId("5c6ecf686fd07954a489068f"),
ObjectId("5c6ecf6f6fd07954a4890690"),
ObjectId("5c6ecf756fd07954a4890691"),
ObjectId("5c6ecf7d6fd07954a4890692"),
ObjectId("5c6ecf8d6fd07954a4890693")
]
}
Display all documents from a collection with the help of find() method ?
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" }
Example: Getting Last N Records
Here is the query to get last 7 records from a collection using $natural and limit() ?
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" }
How It Works
This query returns the last 7 inserted documents in reverse chronological order. The $natural:-1 sorts by insertion order descending, while limit(7) restricts the output to exactly 7 records.
-
$natural:-1− Sorts documents by reverse insertion order (newest first) -
limit(7)− Restricts the result to exactly 7 documents
Conclusion
Use sort({$natural:-1}).limit(N) to retrieve the last N inserted records in MongoDB. The $natural parameter ensures documents are sorted by insertion order, making this an efficient way to get the most recently added records.
