-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusing_upsert.py
38 lines (24 loc) · 897 Bytes
/
using_upsert.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
import pymongo
import sys
# establish a connection to the database
connection = pymongo.MongoClient("mongodb://localhost")
db = connection.test
things = db.things
def using_upsert():
print "updating with upsert"
try:
# lets remove all stuff from things
things.drop()
things.update_one({'thing':'apple'}, {'$set':{'color':'red'}}, upsert=True)
things.update_many({'thing':'banana'}, {'$set':{'color':'yellow'}}, upsert=True)
things.replace_one({'thing':'pear'}, {'color':'green'}, upsert=True)
apple = things.find_one({'thing':'apple'})
print "apple: ", apple
banana = things.find_one({'thing':'banana'})
print "banana: ", banana
pear = things.find_one({'thing':'pear'})
print "pear: ", pear
except Exception as e:
print "Unexpected error:", type(e), e
raise
using_upsert()