Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get values of cursor object by using Python

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? :)

like image 806
Süleyman K Avatar asked Sep 26 '22 21:09

Süleyman K


1 Answers

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"]
like image 99
The6thSense Avatar answered Sep 30 '22 07:09

The6thSense