MongoDB - SPLessons

MongoDB Cursors

Chapter 15

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

MongoDB Cursors

MongoDB Cursors

shape Description

When ever a client get a connection with the server by default it will provides an implicit work area to each client for it’s internal processing of Mongod statements. This work area is dedicated to a specific client. This implicit work area is called as implicit MongoDB Cursors.

A client can access these implicit MongoDB Cursors with the name of mongoDB database. MongoDB Cursors are used for rows repetition returned by a query on a row-by-row process. Mongod commands will function on all the collections at one time in the program. Mongo will populate a cursor and gives a batch of documents each time. A client can then signal the server to close the cursor before all the documents have been exhausted and thereby saving many bytes over the network, and also it will request the server to service for other calls. The shell enumerated all the documents matching the criteria that contains the empty set.


shape Conceptual
figure

Types of MongoDB Cursors

shape Description

MongoDB Cursors can be classified into the following types such as

Sort() cursors

shape Description

The more obvious and popular cursor option is sorting.In sorting the collections will be arranged in ascending or descending order based on the cursor attribute. And these sorting is performed from the server end only.

shape Examples

By viewing the below example, one can easily understand the concept of sort().

MongoDB shell version:2.6.1
connecting to: test
>use test
switched to db test
>show collections
names
system.indexes
>db.names.find();
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c0"), "name":"Alia"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c1"), "name":"Bedo"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c2"), "name":"Chameli"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c3"), "name":"Dev D"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c4"), "name":"Emli"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c5"), "name":"Farshan"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c6"), "name":"Gangs"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c7"), "name":"Hum"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c8"), "name": 25}
>var cur = db.names.find();
>cur
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c0"), "name":"Alia"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c1"), "name":"Bedo"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c2"), "name":"Chameli"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c3"), "name":"Dev D"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c4"), "name":"Emli"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c5"), "name":"Farshan"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c6"), "name":"Gangs"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c7"), "name":"Hum"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c8"), "name": 25}
>cur.sort({"names": -1});
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c7"), "name":"Hum"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c6"), "name":"Gangs"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c5"), "name":"Farshan"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c3"), "name":"Dev D"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c2"), "name":"Chameli"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c1"), "name":"Bedo"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c0"), "name":"Alia"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c8"), "name": 25}

Here in the above example the sort() will arrange the collection_id in descending order. And these sort() will effects only string names not numbers.

Limit() cursors

shape Description

Limit cursor is used for retrieving only specific documents or even all the documents that matching the given criteria. Limit cursor takes a number and returns only that number of documents, if they exist,if less than the limit exists only those would be returned, and the cursor can be made efficient on the server because the server known it doesn’t need to prepare any more documents, it can a creates a much smaller data structures.

shape Examples

By viewing the below example, one can easily understand the concept of limit().

MongoDB shell version:2.6.1
connecting to: test
>use test
switched to db test
>show collections
names
system.indexes
>db.names.find();
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c0"), "name":"Alia"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c1"), "name":"Bedo"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c2"), "name":"Chameli"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c3"), "name":"Dev D"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c4"), "name":"Emli"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c5"), "name":"Farshan"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c6"), "name":"Gangs"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c7"), "name":"Hum"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c8"), "name": 25}
>var cur = db.names.find();
>cur
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c0"), "name":"Alia"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c1"), "name":"Bedo"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c2"), "name":"Chameli"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c3"), "name":"Dev D"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c4"), "name":"Emli"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c5"), "name":"Farshan"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c6"), "name":"Gangs"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c7"), "name":"Hum"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c8"), "name": 25}
>var cur =db.names.find();
>cur.limit(3);
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c0"), "name":"Alia"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c1"), "name":"Bedo"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c2"), "name":"Chameli"}

Here in the above example the limit() will limit the collections in mongoDB program. And the above program has assigned a limit permission to number(3), so it will give the output values of first three collections.

Skip() cursors

shape Description

The skip() function is very useful and it combines both sort() and limit() cursors.And these skip() is very much similar to paging and these skip() perform on the server side end  managing through records. Here by using skip and limit cursors is much more efficient way to page through documents than retrieving all of them into the client and paging on the client side. 

shape Examples

By viewing the below example, one can easily understand the concept of skip().

MongoDB shell version:2.6.1
connecting to: test
>use test
switched to db test
>show collections
names
system.indexes
>db.names.find();
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c0"), "name":"Alia"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c1"), "name":"Bedo"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c2"), "name":"Chameli"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c3"), "name":"Dev D"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c4"), "name":"Emli"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c5"), "name":"Farshan"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c6"), "name":"Gangs"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c7"), "name":"Hum"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c8"), "name": 25}
>var cur = db.names.find();
>cur
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c0"), "name":"Alia"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c1"), "name":"Bedo"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c2"), "name":"Chameli"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c3"), "name":"Dev D"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c4"), "name":"Emli"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c5"), "name":"Farshan"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c6"), "name":"Gangs"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c7"), "name":"Hum"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c8"), "name": 25}
>var cur =db.names.find();
>cur.sort({"name": 1}).limit(5).skip(2);
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c2"), "name":"Chameli"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c3"), "name":"Dev D"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c4"), "name":"Emli"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c5"), "name":"Farshan"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c6"), "name":"Gangs"}

Here in the above example,it will skip the first “2” documents values and limit the document values to “5” so  the skip() cursor will display the remaining document values.

FindOne() cursors

shape Description

In findOne() cursor it will retrieve only one document that is required for the client.when issuing the findone command it will return exactly one record, not a cursor,but a single record.The findOne command operation is very much similar to find command except it returns one record.

shape Examples

By viewing the below example,one can easily understand the concept of findOne().

MongoDB shell version:2.6.1
connecting to: test
>use test
switched to db test
>show collections
names
system.indexes
db.names.findone({_id:})
{
     "_id"  : 1,
     "name" : "Alia"
      "tags": {
              "type" :"humans",
              "colour":"red"
}
}
> c=db.names.findone({_id:})
{
     "_id"  : 1,
     "name" : "Alia"
      "tags": {
              "type" :"humans",
              "colour":"red"
}
}

Here in the above example,it will retrieve only one document value.(i.e., Alia document or record).

Summary

shape Key Points

  • MongoDB Cursors – It will retrieves the rows from the database.
  • Sort() cursors – It will arrange the documents in ascending or descending order.
  • Limit() cursors – It will retrieves the document that is matching the given criteria.
  • Skip() cursors() – Combines both sort() and limit() cursors.
  • FindOne() cursors() – Will retrieves only one document.