Mongodb Cursor Methods and Example

Cursor method in mongoDB

MongoDB Cursor Method with Example


Cursor in MongoDB is defined as a returned Collection Document when find method execution happens. Cursor is like array of a pointer which point specific Index. 

We will perform all the mongoDB Shell Cursor methods on studentInfo DB. Create studentInfo Database in MongoDB server. Use the below code for creating the DB.

  

Using db.createCollection() method, create collection in database. Command to create studnet collection in studentInfo Database is below 


Insert Some Document in student collection where we can apply the MongoDB Collection Methods to explore further usage. I am Inserting five document in Student Collection. Inserted document in student Collection. Below is the document list present in student collection.
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf54"),
        "id" : 1,
        "name" : "M Kumar",
        "dob" : "2003-04-07",
        "marks" : 54,
        "created_at" : "2017-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf55"),
        "id" : 2,
        "name" : "MOHAMMED ISHAQ  H",
        "dob" : "2002-06-14",
        "marks" : 67,
        "created_at" : "2016-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf56"),
        "id" : 3,
        "name" : "ANBUSELVAM S",
        "dob" : "2004-06-18",
        "marks" : 87,
        "created_at" : "2019-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf57"),
        "id" : 4,
        "name" : "MARTINPRIYADOSS J",
        "dob" : "2004-03-08",
        "marks" : 43,
        "created_at" : "2012-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebf58376803d36568082c60"),
        "id" : 5,
        "name" : "JAGAN J",
        "dob" : "2003-04-10",
        "marks" : 98,
        "created_at" : "2012-11-13 23:33:42"
}

MongoDB Cursor Count() Method



Count() Method will return the document count in specified MongoDB collection. O/P of the above query is 5 as per the above document inserted in it.

db.student.find({marks :{$lt : 70}}).count()

Output of the above query will be 3.

MongoDB Cursor Limit() Method

limit() Method in MongoDB will restrict the document count output while we fetch using find method.. 

db.student.find({$or :[{id :{$lt : 2}},{marks :{$gt:80}}] }).pretty()
O/P :
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf54"),
        "id" : 1,
        "name" : "M Kumar",
        "dob" : "2003-04-07",
        "marks" : 54,
        "created_at" : "2017-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf56"),
        "id" : 3,
        "name" : "ANBUSELVAM S",
        "dob" : "2004-06-18",
        "marks" : 87,
        "created_at" : "2019-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebf58376803d36568082c60"),
        "id" : 5,
        "name" : "JAGAN J",
        "dob" : "2003-04-10",
        "marks" : 98,
        "created_at" : "2012-11-13 23:33:42"
}
db.student.find({$or :[{id :{$lt : 2}},{marks :{$gt:80}}] }).limit(2).pretty()

mongoDB cursor Limit method
Output :
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf54"),
        "id" : 1,
        "name" : "M Kumar",
        "dob" : "2003-04-07",
        "marks" : 54,
        "created_at" : "2017-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf56"),
        "id" : 3,
        "name" : "ANBUSELVAM S",
        "dob" : "2004-06-18",
        "marks" : 87,
        "created_at" : "2019-11-13 23:33:42"
}

MongoDB Cursor Pretty() Method 


Cursor Pretty() method will output the fetched collection document in readable format. Without pretty() method all the Collection document will come row by row. Below is the example with and without pretty() method

db.student.find({marks : {$gt : 60}})

Without MongoDB Pretty Method

db.student.find({marks : {$gt : 70}}).pretty()
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf56"),
        "id" : 3,
        "name" : "ANBUSELVAM S",
        "dob" : "2004-06-18",
        "marks" : 87,
        "created_at" : "2019-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebf58376803d36568082c60"),
        "id" : 5,
        "name" : "JAGAN J",
        "dob" : "2003-04-10",
        "marks" : 98,
        "created_at" : "2012-11-13 23:33:42"
}

MongoDB Cursor Sort() Method


Cursor sort() Method in mongoDB hep us to arrange the Specified collection document in sorting order of Ascending or Descending Order. Here Sorting In ascending order is represented by 1 and for descending order it is -1. Below is the Example code , how to use sort() method in MongoDB.

db.student.find({marks :{$lte : 90 }}).sort({marks:1}).pretty()

Cursor Sort() in Ascending Order in MongoDB

Output of the above sort() method code is below as a result. Here in the above code we arranged the document in ascending order based on the mark column.
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf57"),
        "id" : 4,
        "name" : "MARTINPRIYADOSS J",
        "dob" : "2004-03-08",
        "marks" : 43,
        "created_at" : "2012-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf54"),
        "id" : 1,
        "name" : "M Kumar",
        "dob" : "2003-04-07",
        "marks" : 54,
        "created_at" : "2017-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf55"),
        "id" : 2,
        "name" : "MOHAMMED ISHAQ  H",
        "dob" : "2002-06-14",
        "marks" : 67,
        "created_at" : "2016-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf56"),
        "id" : 3,
        "name" : "ANBUSELVAM S",
        "dob" : "2004-06-18",
        "marks" : 87,
        "created_at" : "2019-11-13 23:33:42"
}

Sort() in MongoDB in Descending Order

db.student.find({marks : {$lte : 90}},{name:1,marks:1,_id:1}).sort({name:-1}).pretty()
Above command will arrange the document collection in descending order based on the name. Here -1 is for descending order, name is the document field.
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf55"),
        "name" : "MOHAMMED ISHAQ  H",
        "marks" : 67
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf57"),
        "name" : "MARTINPRIYADOSS J",
        "marks" : 43
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf54"),
        "name" : "M Kumar",
        "marks" : 54
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf56"),
        "name" : "ANBUSELVAM S",
        "marks" : 87
}

Cursor Skip() method in mongoDB


db.student.find({marks:{$lt : 80}},{name:1,marks:1,_id:0}).sort({name:1}).pretty()

This above query will give the output which is below and without skip() method.

{ "name" : "M Kumar", "marks" : 54 }
{ "name" : "MARTINPRIYADOSS J", "marks" : 43 }
{ "name" : "MOHAMMED ISHAQ  H", "marks" : 67 }

Now we will apply limit and get the document.

db.student.find({marks:{$lt : 80}},{name:1,marks:1,_id:0}).sort({name:1}).limit(1).pretty()

Output of the above query will be

{ "name" : "M Kumar", "marks" : 54 }

see the output of both the query, first query have given three document as a output and the second query have given one document because limit is set on the query.

Now we will apply skip() mongoDB Method with limit then we will understand the difference.

db.student.find({marks:{$lt : 80}},{name:1,marks:1,_id:0}).sort({name:1}).limit(1).skip(1)

Output of the above query is below. In the above query limit is set the first record will be skipped and the second record will be taken if available.

{ "name" : "MARTINPRIYADOSS J", "marks" : 43 }

Cursor toArray() Method in MongoDB

toArray() method return all the collection document as an array from the cursor. If we use toArray() method, it will iterate the cursor and load all the data in RAM. In simple word It return an Array of Document.

toArray() Mongo Method Create the json array using all the cursor Document specified which is the easiest way to pass the result to Application.

db.student.find().limit(2).toArray()
.
toArray() method In MongoDB

See the above MongoDB Query where toArray() method have return the document as a array. If you execute the mongo query without toArray() method it will just return the document collection. Below is the output of below query.

db.student.find().limit(2).pretty()

{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf54"),
        "id" : 1,
        "name" : "M Kumar",
        "dob" : "2003-04-07",
        "marks" : 54,
        "created_at" : "2017-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf55"),
        "id" : 2,
        "name" : "MOHAMMED ISHAQ  H",
        "dob" : "2002-06-14",
        "marks" : 67,
        "created_at" : "2016-11-13 23:33:42"
}



No comments:

Post a Comment

Our Feature Post

There is a tree between houses of A and B If the tree leans on As House

    There is a tree between houses of A and B. If the tree There is a tree between houses of A and B. If the tree leans on A’s House, the t...

Our Popular Post