【发布时间】:2019-03-26 11:49:35
【问题描述】:
我使用 collection.update() 将 Document 插入到 Collection 中,因为每个数据我都有一个不同的 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"]}})} 对吗?
【问题讨论】: