-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpymongo_upsert.py
35 lines (24 loc) · 963 Bytes
/
pymongo_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
from pymongo import MongoClient
import sys
# establish a connection to database
connection=MongoClient('localhost',27017)
# Get a handle to school database
db=connection.school
mycol=db.fruits
def upserts():
mycol.drop()
mycol.update_one({'thing':'apple'},{'$set':{'color':'red'}},upsert=True)
mycol.update_many({'thing':'banana'},{'$set':{'color':'yellow'}},upsert=True)
mycol.replace_one({'thing':'peach'},{'color':'green'},upsert=True)
mycol.replace_one({'_id':1001},{'color':'blue'},upsert=True)
apple=mycol.find_one({'thing':'apple'})
print(apple)
banana=mycol.find_one({'thing':'banana'})
print(banana)
peach=mycol.find_one({'thing':'peach'})
idOne=mycol.find_one({'_id':1001})
print(idOne)
'''it will give None as result as 'thing:peach' is a selector in replace_one and color:green is the record to be inserted. If we query the collctn we can find 'color':'green'
value in the col inserted in the run '''
print(peach)
upserts()