Skip to content

Instantly share code, notes, and snippets.

@bchirico
Created November 21, 2013 10:38
Show Gist options
  • Save bchirico/7579473 to your computer and use it in GitHub Desktop.
Save bchirico/7579473 to your computer and use it in GitHub Desktop.
Pymongo find and find_one
from pymongo import MongoClient
import sys
client = MongoClient('localhost', 27017)
db = client.school
score = db.score
def find_one():
print "command find_one"
query = {'student_id': 10}
try:
doc = score.find_one(query)
except:
print "Unexpected error", sys.exc_info()[0]
print doc
def find():
print "command find"
query = {'type': 'exam'}
try:
cursor = score.find(query)
except:
print "Unexpected error", sys.exc_info()[0]
limit = 0
for doc in cursor:
print doc
limit += 1
if (limit > 10):
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment