如何使用 pymongo 的 collection.update_one 或 update_many 指定 unsafe/safe 写入

How to specify a unsafe/safe write with pymongo's collection.update_one or update_many

我的连接默认为 w=0 但是对于 collection.update_one 或 collection.update_many,我想通过设置参数 w=0 来设置每个操作的 write_concern。相反,我收到此错误:

update_one() got an unexpected keyword argument 'w'

正确的做法是什么?我看到 insert 接受 'w' 但不接受 update_one 或 update_many。为什么?

覆盖 PyMongo 客户端、数据库或集合的写关注的新方法是使用 "with_options":

client = MongoClient(w=0)
collection = client.database.collection
w1_collection = collection.with_options(write_concern=WriteConcern(w=1))
w1_collection.update_one({'_id': 1}, {'$inc': {'x': 3}})

请参阅 write concern and with_options

的文档