Created
November 21, 2013 10:38
-
-
Save bchirico/7579473 to your computer and use it in GitHub Desktop.
Pymongo find and find_one
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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