如何插入文档collection.update\u许多()到集合(MongoDB)使用Pymongo(无重复)

2024-04-24 22:49:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我将文档插入收藏集合.更新()因为每个数据我都有一个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"]}})}是对的?你知道吗


Tags: 数据textnameinclientidchilddb