Skip to content Skip to sidebar Skip to footer

How To Iterate Through Every Other Document From A Mongo Db Cursor

I have a mongo DB cursor with documents that I want to create into Dataframes. However, the documents in that cursor can have a runTime that's too close. Therefore I'd like to get

Solution 1:

Use cursor.next() to skip over each alternate cursor result.

As a demonstration:

from pymongo import MongoClient

client = MongoClient()
db = client.test

db.pytest.delete_many({})
db.pytest.insert_many([{ 'value': i+1 } for i,x inenumerate([1] * 10)])

cursor = db.pytest.find({},{ '_id': 0 })

count  = cursor.count()
print count
cursor.next()

for doc in cursor:
  print doc
  count -= 2print count
  if (count > 0):
    cursor.next()

Would return:

10
{u'value': 2}
8
{u'value': 4}
6
{u'value': 6}
4
{u'value': 8}
2
{u'value': 10}
0

The only thing you need to be aware of when calling cursor.next() is that the cursor actually has remaining results before you call it, otherwise you will raise an exception due to the depleted cursor. For this reason you do something like obtain the cursor.count() and then decrement and track the remaining before you decide to issue.

Note that "odd" numbered results would deplete the cursor before the check anyway, so it's really there to make sure you don't advance the cursor on even numbered results when the remaining documents are 0.

Alternate approaches like you partly attempted are to convert the cursor to a list and then you can grab slices, but that means loading all results into memory, which is probably impractical for most result sets of a reasonable size.

Post a Comment for "How To Iterate Through Every Other Document From A Mongo Db Cursor"