-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusing_find.py
49 lines (31 loc) · 923 Bytes
/
using_find.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python
import pymongo
# It is not necessary to import sys
# establish a connection to the database
connection = pymongo.MongoClient("mongodb://localhost")
# get a handle to the school database
db = connection.school
scores = db.scores
def find():
print "find(), reporting for duty"
query = {'type': 'exam'}
try:
cursor = scores.find(query)
except Exception as e:
print "Unexpected error:", type(e), e
sanity = 0
for doc in cursor:
print doc
sanity += 1
if (sanity > 10):
break
def find_one():
print "find_one(), reporting for duty"
query = {'student_id': 10}
try:
doc = scores.find_one(query)
except Exception as e:
print "Unexpected error:", type(e), e
print doc
if __name__ == '__main__':
find() # Change this to find_one() to run that function, instead.