David Dias
David Dias

Reputation: 1832

How to use pymongo to process huge collections

I was looking all over the web for my problem but kind of got stuck, I'm trying to use pymongo to go over a huge collection on MongoDB using cursors, however it seams that there is no hasNext() implementation in python like JS.

Here is my code:

cursor = news.find() 
while(cursor.hasNext()):    
     doc = cursor.next()

Upvotes: 0

Views: 175

Answers (2)

feifan.overflow
feifan.overflow

Reputation: 565

you can go over the collection by for statement

for record in cursor:
  print record

The for keyword actually calls _iter_() and next() for you. http://docs.python.org/2/library/collections.html#collections.Iterator

Cursor object in pymongo come with these 2 methods. https://github.com/mongodb/mongo-python-driver/blob/master/pymongo/cursor.py#L1010

Upvotes: 1

David Dias
David Dias

Reputation: 1832

Got it!

pymongo has no hasNext(), instead the method next() returns None if there is no more objects , so this is the trick

Thanks eitherway!

Upvotes: 1

Related Questions