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.
Description
MongoDB Cursors can be classified into the following types such as
- Sort()
- Limit()
- Skip()
- FindOne()
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.
Examples
By viewing the below example, one can easily understand the concept of sort().
[c]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}
[/c]
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.
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.
Examples
By viewing the below example, one can easily understand the concept of limit().
[c]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"}
[/c]
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.
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.
Examples
By viewing the below example, one can easily understand the concept of skip().
[c]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"}
[/c]
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.
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.
Examples
By viewing the below example,one can easily understand the concept of findOne().
[c]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"
}
}
[/c]
Here in the above example,it will retrieve only one document value.(i.e., Alia document or record).
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.