-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpymongo_savingdata.py
More file actions
50 lines (40 loc) · 1.19 KB
/
Copy pathpymongo_savingdata.py
File metadata and controls
50 lines (40 loc) · 1.19 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
41
42
43
44
45
46
47
48
49
50
import sys
import pymongo
def main():
connection = pymongo.MongoClient("mongodb://localhost")
db = connection.test
people = db.people
person = { 'name':'Tim Taylor', 'job':'Handy Man',
'address': { 'address1':'Some Address',
'street':'Some Street',
'state':'Some State',
'city':'Some City'},
'interests':['fixing stuff', 'breaking stuff', 'cars']}
print "\nFirst insert\n"
print(person)
try:
people.insert(person)
except:
print "\nFirst insert failed: ", sys.exc_info()[0]
# At this point person has been updated with an _id field which should cause the second
# insert to fail.
print "\nSecond Insert\n"
print(person)
try:
people.insert(person)
except:
print "\nSecond insert failed: ", sys.exc_info()[0]
# If we remove the id field the record can be inserted which will create two
# records with different ids.
del(person['_id'])
print "\nThird Insert\n"
print(person)
try:
people.insert(person)
except:
print "\nThird insert failed: ", sys.exc_info()[0]
print "\nSaved records:\n"
for record in people.find():
print(record)
people.remove(record)
main()