-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusing_insert_many.py
More file actions
40 lines (32 loc) · 1.13 KB
/
using_insert_many.py
File metadata and controls
40 lines (32 loc) · 1.13 KB
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
#!/usr/bin/env python
import pymongo
# establish a connection to the database
connection = pymongo.MongoClient("mongodb://localhost")
def insert_many():
# get a handle to the school database
db = connection.school
people = db.people
print "insert_many, reporting for duty"
andrew = {"_id": "erlichson", "name": "Andrew Erlichson",
"company": "MongoDB",
"interests": ['running', 'cycling', 'photography']}
richard = {"name": "Richard Kreuter", "company": "MongoDB",
"interests": ['horses', 'skydiving', 'fencing']}
people_to_insert = [andrew, richard]
try:
people.insert_many(people_to_insert, ordered=False)
except Exception as e:
print "Unexpected error:", type(e), e
def print_people():
# get a handle to the school database
db = connection.school
people = db.people
cur = people.find({}, {'name': 1})
for doc in cur:
print doc
if __name__ == '__main__':
print "Before the insert_many, here are the people"
print_people()
insert_many()
print "\n\nAfter the insert_many, here are the people"
print_people()