To get the last inserted record from a MongoDB collection, you can use the find()
method in combination with the sort()
and limit()
methods.
By default, MongoDB assigns a unique objectId
to each document, which is created in ascending order. So, if we want to get the last inserted record, we have to first sort all the documents in descending order by the _id
key using the sort()
method and then apply the limit(1)
method to limit the number of records to one.
The sort()
method in MongoDB is used to sort the results of a query in ascending or descending order based on one or more fields. Whereas, the limit()
method is used to limit the number of records returned by a query.
Below is the query to get the last inserted record:
db.collectionName.find().sort({ '_id': -1 }).limit(1)
Example:
Let’s first insert a few records into the student
collection using the insertMany()
method:
db.student.insertMany([ { "name": "arun", "marks": 76 }, { "name": "rajesh", "marks": 87 }, { "name": "moorthy", "marks": 54 }, { "name": "raja", "marks": 73 }, { "name": "usha", "marks": 97 }, { "name": "priya", "marks": 49 }, { "name": "Sundar", "marks": 75 }, { "name": "Kavitha", "marks": 86 }, { "name": "Dinesh", "marks": 92 }, { "name": "Hema", "marks": 50 } ])
The below image shows that all the records were successfully inserted into the student collection.
Now, to get the last inserted record from the student collection, we can use the following query:
db.student.find().sort({ '_id': -1 }).limit(1)
Output:
After running the above query, we get the last inserted record from the student collection as follows:
{ _id: ObjectId("63d35564cf5e3174e563afb1"), name: 'Hema', marks: 50 }
Get the Last N Records from a Collection
To get the last N records from a MongoDB collection, you can pass the number N as a parameter to the limit()
method, provided that the matching records are in descending order of the _id
field.
Use the following query to get the last N records from a collection:
db.collectionName.find().sort({ '_id': -1 }).limit(N)
For example, if we want to get the last 5 records from the student
collection(discussed above), we can pass the number 5 to the limit()
method as a parameter.
db.student.find().sort({ '_id': -1 }).limit(5)
Output:
The above query returns the last 5 records inserted into the student
collection as follows:
{ _id: ObjectId("63d35564cf5e3174e563afb1"), name: 'Hema', marks: 50 } { _id: ObjectId("63d35564cf5e3174e563afb0"), name: 'Dinesh', marks: 92 } { _id: ObjectId("63d35564cf5e3174e563afaf"), name: 'Kavitha', marks: 86 } { _id: ObjectId("63d35564cf5e3174e563afae"), name: 'Sundar', marks: 75 } { _id: ObjectId("63d35564cf5e3174e563afad"), name: 'priya', marks: 49 }
Conclusion
In this article, we learned how we can get the last inserted record from a MongoDB collection.
In summary, to get the last inserted record from a MongoDB collection, you can use the find()
method in combination with the sort()
and limit()
methods.
You have to first sort the result in the descending order of the _id
field and then pass 1 as a parameter to the limit()
method. The limit(1)
will limit the result to one record only.
Thanks for reading.