Open In App

How to Get the Last N Records in MongoDB?

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In MongoDB, the developers sometimes encounter the challenge of retrieving the last N records to access real-time information efficiently. To solve this problem, we explore effective methods that fast data retrieval and make it simple for users to access the latest insights easily.

In this article, We will learn about How to get the last N records in MongoDB MongoDB

How to Get the Last N Records in MongoDB?

Retrieving the last N records from MongoDB collections is a common problem faced by developers to fetch real-time information. To overcome this problem, we will discuss efficient methods that simplify data retrieval and allow users to access information easily. Below is the method that helps us to get the last N records in MongoDB is as follows:

  1. Using the find() Method with sort()
  2. Using the skip() method with the count() Method
  3. Using the Aggregation Pipeline to Get the last N records in MongoDB

Let's set up an Environment:

To understand How to Find Duplicates in MongoDB we need a collection and some documents on which we will perform various operations and queries. Here we will consider a collection called yourCollectionName which contains information such as _id, name, age, department, and join_date in various documents.

db.yourCollectionName.insertMany([
{ "_id": 1, "name": "Alice", "age": 30, "department": "HR", "join_date": "2023-03-01" },
{ "_id": 2, "name": "Bob", "age": 35, "department": "Engineering", "join_date": "2022-08-15" },
{ "_id": 3, "name": "Charlie", "age": 28, "department": "Marketing", "join_date": "2024-01-10" },
{ "_id": 4, "name": "David", "age": 32, "department": "Finance", "join_date": "2023-11-20" },
{ "_id": 5, "name": "Eve", "age": 29, "department": "Engineering", "join_date": "2023-05-05" }
]);

Output:

sampleCollection
sample collection

1. Using the Find() Method With Sort()

In this method , We will use the find() function to retrieve all documents from the collection, then sorts them in descending order based on their natural order of insertion ($natural). Finally, it limits the result to N documents using the limit() function.

Let's Retrieve the last three records from the collection yourCollectionName.

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

Output:

using-Find

Explanation: This query retrieves the last 3 records from the collection yourCollectionName by sorting the documents in descending order based on their natural order of insertion ($natural) and then limiting the result to 3 documents.

2. Using Count() and Skip() Method

One more approach is to use the count() method to find out how many documents are in the collection and then use the skip() method to get a certain number of documents before getting the last N records.

Let's count the number of documents in the collection:

db.yourCollectionName.count();

This will return the count of documents in the collection. so we inserted 5 documents previously, it will return 5.

Now retrieve the last three documents from the collection:

db.yourCollectionName.find().skip(db.yourCollectionName.count() - 3).pretty();

Output:

count-and-skip
Using Count and Skip method

Explanation: In the above query we counts the total number of documents in the collection. Then retrieves the last three documents from the collection by skipping the appropriate number of documents.

3. Using the Aggregation Pipeline to Get the last N records in MongoDB

To get the last N records in MongoDB using the aggregation pipeline, we can use the $sort and $limit stages. Suppose we want to get the records based on the join_date and we can sort the records in descending order by join_date and then limit the results to N records.

db.students.aggregate([
{
$sort: { join_date: -1 }
},
{
$limit: 3
}
])

Output:

[
{
"_id": 3,
"name": "Charlie",
"age": 28,
"department": "Marketing",
"join_date": "2024-01-10"
},
{
"_id": 4,
"name": "David",
"age": 32,
"department": "Finance",
"join_date": "2023-11-20"
},
{
"_id": 5,
"name": "Eve",
"age": 29,
"department": "Engineering",
"join_date": "2023-05-05"
}
]

Conclusion

Overall, In MongoDB, fetching the last N records is a common challenge for developers. With the help of efficient methods like using the find() Method with sort() and the Count() and Skip() Method, data retrieval becomes easy. By following these approaches, developers can easily access the latest information.


Article Tags :

Similar Reads