Jake Stokes Jake Stokes - 9 months ago 53
Python Question

Simple script not printing

I'm trying to sort a collection, and then print the first 5 docs to make sure it has worked:

#!/user/bin/env python

import pymongo

# Establish a connection to the mongo database.
connection = pymongo.MongoClient('mongodb://localhost')

# Get a handle to the students database.
db = connection.school
students = db.students

def order_homework():

projection = {'scores': {'$elemMatch': {'type': 'homework'}}}
cursor = students.find({}, projection)

# Sort each item's scores.
for each in cursor:

# Sort by _id.
cursor = sorted(cursor, key=lambda x: x['_id'])

# Print the first five items.
count = 0
for each in cursor:
count += 1
if count == 5:

if __name__ == '__main__':

When I run this, nothing prints.

If I take out the sorts, then it prints.

Each sort works when run individually.

Please teach me what I'm doing wrong / educate me.
Thank you.

Answer Source

You're trying to treat the cursor like a list, which you can iterate several times from the start. PyMongo cursors don't act that way - once you've iterated it in for each in cursor, the cursor is completed and you can't iterate it again.

You can turn the cursor into a list like:

data = list(students.find({}, projection))

For efficiency, get results pre-sorted from MongoDB:

list(students.find({}, projection).sort('_id'))

This sends the sort criterion to the server, which then streams the results back to you pre-sorted, instead of requiring you to do it client-side. Now delete your "Sort by _id" line below.