5

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

11
  • did you try looping over gettingTotalSize since it returns a cursor you can loop over it Commented Aug 31, 2015 at 6:32
  • like? i tried something and didnt work Commented Aug 31, 2015 at 7:03
  • Could you post what you have tried and what you got Commented Aug 31, 2015 at 7:04
  • i tried lots of :) one of them; for x in range(0, gettingTotalSize ): print gettingTotalSize [x] AND result: EXPECTION: no such item for Cursor instance Commented Aug 31, 2015 at 7:06
  • How about for row in gettingTotalSize :print row Commented Aug 31, 2015 at 7:07

1 Answer 1

4

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"]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.