我想用pymongo更新mongoDB数据库中的多个文档。我有这个数据:
data_to_be_updated = [
{"sourceID" : 6, "source" : "test", "name" : "simon"},
{"sourceID" : 8, "source" : "test", "name" : "greg"},
{"sourceID" : 9, "source" : "test", "name" : "julie"},
{"sourceID" : 10, "source" : "test", "name" : "john"}
]
sourceIDs = [6, 8, 9, 10]
我想更新data_to_be_inserted
中的每个元素,按它们的sourceID
过滤它们。我尝试过使用update_many
函数,但是它更新了所有与单个过滤器匹配的文档。我当然可以使用这样的for循环:
上面的方法用于许多调用。如何在对数据库的单个调用中实现相同的目标?在
使用bulk_write。如下所示,具体取决于需要更新哪些字段:
相关问题 更多 >
编程相关推荐