Python - Pymongo Insert and Update Documents
Use upsert
option:
from pymongo import MongoClient
cl = MongoClient()
coll = cl["local"]["test2"]
data = [{"_id" : 1, "foo" : "HELLO"}, {"_id" : 2, "Blah" : "Bloh"}]
for d in data:
coll.update({'_id':d['_id']}, d, True)
You can also use save
import pymongo
con = pymongo.MongoClient()
coll = con.db_name.collection_name
docs = [{"_id" : 1, "foo" : "HELLO"}, {"_id" : 2, "Blah" : "Bloh"}]
for doc in docs:
coll.save(doc)