Answer the question
In order to leave comments, you need to log in
How to add records in mongodb only if they don't exist?
import pymongo
...
DATA=[{'title': 'sTitle1', 'info': 'sInfo1'}, {'title': 'sTitle2', 'info': 'sInfo2'}]
...
db = client.get_default_database()
yamaha = db['yamaha']
yamaha.insert_many([DATA])
...
DATA2=[{'title': 'sTitle3', 'info': 'sInfo3'}, {'title': 'sTitle2', 'info': 'sInfo2'}]
Answer the question
In order to leave comments, you need to log in
Here we compare by specific field
for item in DATA2:
yamaha.update({'title': item['title']}, {'$set': item}, upsert=True)
for item in DATA2:
search_result = yamaha.find_one(item)
if search_result is None:
yamaha.insert(item)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question