Я вставляю Документ в коллекцию с collection.update (), потому что у меня есть разные данные postID
.Я хочу, чтобы при запуске, если сообщение было вставлено в MongoDB, сообщение будет обновлено (не вставлять новое сообщение с postID
, перекрывающимся с postID
первым).Это структура моих данных:
comment1 = [
{
'commentParentId': parent_content.text,
'parentId': parent_ID,
'posted': child_time.text,
'postID':child_ID,
'author':
{
'name': child_name.text
},
'content': child_content.text
},
...............
]
Это мой код, я использовал для вставки данных:
client = MongoClient()
db = client['comment_data2']
db.collection_data = db['comments']
for i in data_comment:
db.collection_data.update_many(
{db.collection_data.find({"postID": {"$in": i["postID"]}})},
{"$set": i},
{'upsert': True}
)
Но у меня ошибка: TypeError: filter must be an instance of dict, bson.son.SON, or other type that inherits from collections.Mapping
в строке {'upsert': True}
.И {db.collection_data.find({"postID": {"$in": i["postID"]}})}
верно?