I insert data to document using pymongo :
client = MongoClient()
db = client['comment_data2']
collection_data = db['comments']
collection_data.insert_many(data_comment)
Each data I have an "postid" to distinguish, like:
comment1 = {
'commentParentId': parent_content.text,
'parentId': parent_ID,
'posted': child_time.text,
'postID':child_ID,
'author':
{
'name': child_name.text
},
'content': child_content.text
}
I run my code more than 2 times and I see my data with duplicates. I want to when I run my code, each postID juch insert 1 times. I'm processing learn pymongo so I don't have any idea for solution. I find a solution in another post on StackOverFlow:
get_db().users.update(
{'_id':ObjectId(session['user_id'])},
{
'$addToSet':{
'hme':ObjectId(id)
}
},
upsert=True
)
But I dont know what does it mean.
Source : https://stackoverflow.com/questions/31043412/insert-not-duplicate-data-with-pymongo-in-mongodb
UPDATE1 :
I have tried to use collection_data.update_many(data_comment, upsert=True)
, but appear an Error : update_many() missing 1 required positional argument: 'update'