Friday 20 February 2015

Cursors


Cursor is a pointer to the result set of a query.

When you query database from mongo shell, it create and returns cursor to mongo shell. Mongo shell iterates and prints cursor data by default.

Note:
in the mongo shell, if the returned cursor is not assigned to a variable using the var keyword, then the cursor is automatically iterated up to 20 times to print up to the first 20 documents in the results.

> db.employee.find()
{ "_id" : 1, "firstName" : "Joel", "lastName" : "chelli", "salary" : 25000, "hobbies" : [ "watching movies", "playing games" ] }
{ "_id" : 2, "firstName" : "Ananad", "lastName" : "Bandaru", "salary" : 200000, "hobbies" : [ "car drivinig", "watching movies" ] }
{ "_id" : 3, "firstName" : "Gopi", "lastName" : "Battu", "salary" : 65000, "hobbies" : [ "climbing hills", "dancing", "singing" ] }
{ "_id" : 4, "firstName" : "Ritwik", "lastName" : "Mohenthy", "salary" : 50000, "hobbies" : [ "watching movies", "watching movies", "watching movies" ] }
{ "_id" : 5, "firstName" : "RamaKrishna", "lastName" : "Gurram", "salary" : 150000, "hobbies" : [ "hackning sites", "Reading books", "listening music" ] }
{ "_id" : 6, "firstName" : "BalaKrishna", "lastName" : "Gurram", "salary" : 80000, "hobbies" : [ "roaming around", "eating food" ] }
{ "_id" : ObjectId("54ba2de1f1c8f2149c000740"), "name" : "Krishna", "project" : "A350" }
{ "_id" : ObjectId("54ba2dfcf1c8f2149c000741"), "name" : "Krishna", "project" : 4 }
{ "_id" : ObjectId("54ba47c9f1c8f2149c000742"), "name" : "Keerthi", "hobbies" : [ "watching movies", "playing games", "cooking" ] }


Manually iterating cursor
Step 1: Assign query result to cursor.
Ex:
var cursor = db.employee.find()

step 2: Iterate cursor
cursor.hasNext() method returns true, if cursor has elements.
Cursor.next() method returns next element.

> var cursor = db.employee.find()
>
> cursor.hasNext()
true
> cursor.next()
{
        "_id" : 1,
        "firstName" : "Joel",
        "lastName" : "chelli",
        "salary" : 25000,
        "hobbies" : [
                "watching movies",
                "playing games"
        ]
}
>
> cursor.hasNext()
true
> cursor.next()
{
        "_id" : 2,
        "firstName" : "Ananad",
        "lastName" : "Bandaru",
        "salary" : 200000,
        "hobbies" : [
                "car drivinig",
                "watching movies"
        ]
}
>
> cursor.hasNext()
true
> cursor.next()
{
        "_id" : 3,
        "firstName" : "Gopi",
        "lastName" : "Battu",
        "salary" : 65000,
        "hobbies" : [
                "climbing hills",
                "dancing",
                "singing"
        ]
}


You can loop through cursor using while loop.

> var cursor = db.employee.find()
>
> while(cursor.hasNext()) printjson(cursor.next())
{
        "_id" : 1,
        "firstName" : "Joel",
        "lastName" : "chelli",
        "salary" : 25000,
        "hobbies" : [
                "watching movies",
                "playing games"
        ]
}
{
        "_id" : 2,
        "firstName" : "Ananad",
        "lastName" : "Bandaru",
        "salary" : 200000,
        "hobbies" : [
                "car drivinig",
                "watching movies"
        ]
}
{
        "_id" : 3,
        "firstName" : "Gopi",
        "lastName" : "Battu",
        "salary" : 65000,
        "hobbies" : [
                "climbing hills",
                "dancing",
                "singing"
        ]
}
{
        "_id" : 4,
        "firstName" : "Ritwik",
        "lastName" : "Mohenthy",
        "salary" : 50000,
        "hobbies" : [
                "watching movies",
                "watching movies",
                "watching movies"
        ]
}
{
        "_id" : 5,
        "firstName" : "RamaKrishna",
        "lastName" : "Gurram",
        "salary" : 150000,
        "hobbies" : [
                "hackning sites",
                "Reading books",
                "listening music"
        ]
}
{
        "_id" : 6,
        "firstName" : "BalaKrishna",
        "lastName" : "Gurram",
        "salary" : 80000,
        "hobbies" : [
                "roaming around",
                "eating food"
        ]
}
{
        "_id" : ObjectId("54ba2de1f1c8f2149c000740"),
        "name" : "Krishna",
        "project" : "A350"
}
{
        "_id" : ObjectId("54ba2dfcf1c8f2149c000741"),
        "name" : "Krishna",
        "project" : 4
}
{
        "_id" : ObjectId("54ba47c9f1c8f2149c000742"),
        "name" : "Keerthi",
        "hobbies" : [
                "watching movies",
                "playing games",
                "cooking"
        ]
}


Sort cursor results
You can sort cursor results.

cursor.sort({firstName : -1})
sort the results of the cursor in ascending order of field firstName.

cursor.sort({firstName : 1})
sort the results of the cursor in descending order of field firstName.

> var cursor = db.employee.find()
> cursor.sort({firstName : -1})
{ "_id" : 4, "firstName" : "Ritwik", "lastName" : "Mohenthy", "salary" : 50000, "hobbies" : [ "watching movies", "watching movies", "watching movies" ] }
{ "_id" : 5, "firstName" : "RamaKrishna", "lastName" : "Gurram", "salary" : 150000, "hobbies" : [ "hackning sites", "Reading books", "listening music" ] }
{ "_id" : 1, "firstName" : "Joel", "lastName" : "chelli", "salary" : 25000, "hobbies" : [ "watching movies", "playing games" ] }
{ "_id" : 3, "firstName" : "Gopi", "lastName" : "Battu", "salary" : 65000, "hobbies" : [ "climbing hills", "dancing", "singing" ] }
{ "_id" : 6, "firstName" : "BalaKrishna", "lastName" : "Gurram", "salary" : 80000, "hobbies" : [ "roaming around", "eating food" ] }
{ "_id" : 2, "firstName" : "Ananad", "lastName" : "Bandaru", "salary" : 200000, "hobbies" : [ "car drivinig", "watching movies" ] }
{ "_id" : ObjectId("54ba2dfcf1c8f2149c000741"), "name" : "Krishna", "project" : 4 }
{ "_id" : ObjectId("54ba2de1f1c8f2149c000740"), "name" : "Krishna", "project" : "A350" }
{ "_id" : ObjectId("54ba47c9f1c8f2149c000742"), "name" : "Keerthi", "hobbies" : [ "watching movies", "playing games", "cooking" ] }
>
>
> var cursor = db.employee.find()
> cursor.sort({firstName : 1})
{ "_id" : ObjectId("54ba2dfcf1c8f2149c000741"), "name" : "Krishna", "project" : 4 }
{ "_id" : ObjectId("54ba2de1f1c8f2149c000740"), "name" : "Krishna", "project" : "A350" }
{ "_id" : ObjectId("54ba47c9f1c8f2149c000742"), "name" : "Keerthi", "hobbies" : [ "watching movies", "playing games", "cooking" ] }
{ "_id" : 2, "firstName" : "Ananad", "lastName" : "Bandaru", "salary" : 200000, "hobbies" : [ "car drivinig", "watching movies" ] }
{ "_id" : 6, "firstName" : "BalaKrishna", "lastName" : "Gurram", "salary" : 80000, "hobbies" : [ "roaming around", "eating food" ] }
{ "_id" : 3, "firstName" : "Gopi", "lastName" : "Battu", "salary" : 65000, "hobbies" : [ "climbing hills", "dancing", "singing" ] }
{ "_id" : 1, "firstName" : "Joel", "lastName" : "chelli", "salary" : 25000, "hobbies" : [ "watching movies", "playing games" ] }
{ "_id" : 5, "firstName" : "RamaKrishna", "lastName" : "Gurram", "salary" : 150000, "hobbies" : [ "hackning sites", "Reading books", "listening music" ] }
{ "_id" : 4, "firstName" : "Ritwik", "lastName" : "Mohenthy", "salary" : 50000, "hobbies" : [ "watching movies", "watching movies", "watching movies" ] }
>


You can limit the cursor results by limit function.
> var cursor = db.employee.find()
> cursor.limit(4)
{ "_id" : 1, "firstName" : "Joel", "lastName" : "chelli", "salary" : 25000, "hobbies" : [ "watching movies", "playing games" ] }
{ "_id" : 2, "firstName" : "Ananad", "lastName" : "Bandaru", "salary" : 200000, "hobbies" : [ "car drivinig", "watching movies" ] }
{ "_id" : 3, "firstName" : "Gopi", "lastName" : "Battu", "salary" : 65000, "hobbies" : [ "climbing hills", "dancing", "singing" ] }
{ "_id" : 4, "firstName" : "Ritwik", "lastName" : "Mohenthy", "salary" : 50000, "hobbies" : [ "watching movies", "watching movies", "watching movies" ] }

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment