How to fetch last n records from a collection in mongodb

This tutorial guides you how to fetch last n records from a collection in MongoDB.

Fetch last n records from collection in mongodb

First, use the following dump names.csv to create sample data in mongodb with collection name “names”  for our exercise or you can insert documents in to collection using db.collection.insertOne() method.

> db.names.insertOne({name:"Krish", gender:"M", count:"2"});

Response:
------------
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e6bcf596fd07954afc2d18d")
}

You can use csv file downloaded from above link and import csv data in to mongodb. To do this follow tutorial on How to import CSV using mongoimport command in MongoDB.

Once CSV file is imported successfully, you can use the following command to display all documents from collection.

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

Response:
-------------------

{
        "_id" : ObjectId("5e4a6df5ab415e1ab85ad526"),
        "name" : "Emma",
        "gender" : "F",
        "count" : "18688"
}
{
        "_id" : ObjectId("5e4a6df5ab415e1ab85ad527"),
        "name" : "Olivia",
        "gender" : "F",
        "count" : "17921"
}
{
        "_id" : ObjectId("5e4a6df5ab415e1ab85ad528"),
        "name" : "Ava",
        "gender" : "F",
        "count" : "14924"
}
{
        "_id" : ObjectId("5e4a6df5ab415e1ab85ad529"),
        "name" : "Isabella",
        "gender" : "F",
        "count" : "14464"
}
{
        "_id" : ObjectId("5e4a6df5ab415e1ab85ad52a"),
        "name" : "Sophia",
        "gender" : "F",
        "count" : "13928"
}
{
        "_id" : ObjectId("5e4a6df5ab415e1ab85ad52b"),
        "name" : "Charlotte",
        "gender" : "F",
        "count" : "12940"
}

--------------

--------------

Then, you can use the following command to fetch the last n records from a collection in mongodb. To specify “n” value you need to use limit() method as shown below.

Note, let say if you wanted to fetch the last 5 records, then you can use sort() in conjunction with limit(). And for ascending or descending sort, you can also specify sort parameter { $natural : 1 } to force the query to perform a forwards collection scan, or { $natural : -1 } for a reverse collection scan.

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

Response:
---------------

{ "_id" : ObjectId("5e6b249f57f109872fc2d1ae"), "name" : "krish", "gender" : "M"
, "count" : "2" }
{ "_id" : ObjectId("5e4a6df9ab415e1ab85b5247"), "name" : "Zzyzx", "gender" : "M"
, "count" : "5" }
{ "_id" : ObjectId("5e4a6df9ab415e1ab85b5246"), "name" : "Zyron", "gender" : "M"
, "count" : "5" }
{ "_id" : ObjectId("5e4a6df9ab415e1ab85b5245"), "name" : "Zyrie", "gender" : "M"
, "count" : "5" }
{ "_id" : ObjectId("5e4a6df9ab415e1ab85b5244"), "name" : "Zyran", "gender" : "M"
, "count" : "5" }

Let’s say you wanted to fetch first 5 records from a collection in mongodb, then you can use the following command.

> db.names.find().sort({$natural:1}).limit(5);

Response:
----------------

{ "_id" : ObjectId("5e4a6df5ab415e1ab85ad526"), "name" : "Emma", "gender" : "F",
 "count" : "18688" }
{ "_id" : ObjectId("5e4a6df5ab415e1ab85ad527"), "name" : "Olivia", "gender" : "F
", "count" : "17921" }
{ "_id" : ObjectId("5e4a6df5ab415e1ab85ad528"), "name" : "Ava", "gender" : "F",
"count" : "14924" }
{ "_id" : ObjectId("5e4a6df5ab415e1ab85ad529"), "name" : "Isabella", "gender" :
"F", "count" : "14464" }
{ "_id" : ObjectId("5e4a6df5ab415e1ab85ad52a"), "name" : "Sophia", "gender" : "F
", "count" : "13928" }

Also See

References

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments