我将文档插入收藏集合.更新()因为每个数据我都有一个postID
到不同。我想在运行时,如果在MongoDB中插入了一个post,那么该post将被更新(而不是插入一个新post,其中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"]}})}
是对的?你知道吗
您可以使用以下代码:
相关问题 更多 >
编程相关推荐