MongoDB – Get the last N records OR most recent records
Today in this article, we will learn to perform MongoDB – Get the last N records OR most recent records. There are easy approaches to achieving the last N records or most records in the mongo document.
We will cover a few approaches to Get the last N records OR most recent records.
Overall, We will cover the below aspects,
I have below Mongo Collection where my fields are defined as below.
I have a field name called “UserID” which is of type “Int“. I want to use this field to query first “N” element from MongoDB.
Command:
Command to get the first N element in MongoDB
db.<Collection>.find().sort({<field>:1}).limit(N)
The above query will give records based on Ascending order of field value.
db.<Collection>.find().sort({<field>:-1}).limit(N)
Similarly, the above query will give records based on descending order of field value.
Example :
Get the last 2 records in MongoDB
Ascending order,
db.Books.find().sort({UserId:1}).limit(2)
OR
Descending Order,
db.Books.find().sort({UserId:-1}).limit(2)
db.Books.find().sort({UserId:1}).limit(2) { "_id" : ObjectId("62c5dd1902514a766441e2ee"), "UserId" : 555, "Books" : [ { "book_name" : "book_name1", "price" : "88", "category" : [ { "key" : "Test1", "value" : 123 } ] }, { "book_name" : "book_name2", "price" : "50", "category" : [ { "key" : "test2-1", "value" : null }, { "key" : "test4", "value" : null } ] } ], "value" : null } { "_id" : ObjectId("6382cfa8c592b1532c8d5bcb"), "UserId" : 934, "DateAdded" : ISODate("2022-01-01T05:00:00Z"), "Books" : [ { "book_name" : "book_name1", "price" : null, "category" : [ { "key" : "Test1", "value" : null } ] }, { "book_name" : "book_name2", "price" : "50", "category" : [ { "key" : "test
Get the last 50 records in MongoDB
db.Books.find().sort({UserId:-1}).limit(50)
Get the last 100 records in MongoDB
db.Books.find().sort({UserId:-1}).limit(100)
Command to get the recent record in MongoDB
Command:
Command to get the most recent records in MongoDB
If you have a date field already in the MongoDB document then you can sort the most recent documents based on dates.
db.<Collection>.find().sort({<DateField>:-1}).limit(N)
OR
db.<Collection>.find().sort({_id:-1}).limit(N)
If your Mongo records use default ObjectID, then this _id will have a default insertion time that is unique and can be used to get records.
Example
db.Books.find().sort({DateAdded:1}).limit(5)
OR
db.Books.find().sort({_id : -1}).limit(5)
db.Books.find().sort({DateAdded:1}).limit(5)
{ “_id” : ObjectId(“62c5dd1902514a766441e2ee”), “UserId” : 555, “Books” : [ { “book_name” : “book_name1”, “price” : “88”, “category” : [ { “key” : “Test1”, “value” : 123 } ] }, { “book_name” : “book_name2”, “price” : “50”, “category” : [ { “key” : “test2-1”, “value” : null }, { “key” : “test4”, “value” : null } ] } ], “value” : null }
{ “_id” : ObjectId(“62c5e3f602514a766441e2ef”), “UserId” : 6777, “DateAdded” : ISODate(“2019-01-01T05:00:00Z”), “Books” : [ { “book_name” : “book_name1”, “price” : null, “category” : [ { “key” : “Test1”, “value” : null } ] }, { “book_name” : “book_name2”, “price” : “50”, “category” : [ { “key” : “test3”, “value” : null }, { “key” : “test4”, “value” : null } ] } ], “value” : null }
{ “_id” : ObjectId(“6382cfbec592b1532c8d5bcc”), “UserId” : 6777, “DateAdded” : ISODate(“2021-01-01T05:00:00Z”), “Books” : [ { “book_name” : “book_name1”, “price” : null, “category” : [ { “key” : “Test1”, “value” : null } ] }, { “book_name” : “book_name2”, “price” : “50”, “category” : [ { “key” : “test3”, “value” : null }, { “key” : “test4”, “value” : null } ] } ], “value” : null }
{ “_id” : ObjectId(“6382cfa8c592b1532c8d5bcb”), “UserId” : 934, “DateAdded” : ISODate(“2022-01-01T05:00:00Z”), “Books” : [ { “book_name” : “book_name1”, “price” : null, “category” : [ { “key” : “Test1”, “value” : null } ] }, { “book_name” : “book_name2”, “price” : “50”, “category” : [ { “key” : “test3”, “value” : null }, { “key” : “test4”, “value” : null } ] } ], “value” : null }
If you don’t have a Date field, you can still use the inbuilt default date object for getting recent documents.
Please refer to this article for more details,
Do you have any comments or ideas or any better suggestions to share?
Please sound off your comments below.
Happy Coding !!
Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.