How to get values of cursor object by using Python

10,398

To iterate over a cursor you could loop over the cursor and to get element out of a dictionary you could pass it's key to get value

Code:

for x in gettingTotalSize :
    print x["c"]
Share:
10,398
Süleyman K
Author by

Süleyman K

Updated on June 04, 2022

Comments

  • Süleyman K
    Süleyman K almost 2 years

    I have data in mondoDB;

    db.np_tpy_gla.find({},{"_id":0, "c": 1})
    

    Result:

    { "c" : NumberLong(18) }
    { "c" : NumberLong(40) }
    { "c" : NumberLong(42) }
    { "c" : NumberLong(54) }
    ...
    

    I am trying to get these values by using Python (pymongo). Here is my code:

    counterNumber = cursor.count()
    gettingTotalSize = cursor.find({"c": True})
    
    print counterNumber 
    print gettingTotalSize 
    

    and here is result:

    115
    <pymongo.cursor.Cursor object at 0x13c7890>
    

    I'm tring to get "gettingTotalSize" values one by one.

    How can i get these values? i also tried for loop.

    Thanks.

    EDIT :

    I changed my codes like:

    gettingTotalSize = cursor.find({}, {"_id": 0, "c": 1})
    

    Vignesh Kalai'codes:

    for x in gettingTotalSize :
        print x
    

    Here is the new result :

    {u'c': 18L}
    {u'c': 40L}
    {u'c': 42L}
    {u'c': 54L}
    ...
    

    Now I need only the value (18,40,42,54...)

    Any ideas? :)