如何使用pymongo的collection.update_one或update_many指定不安全/安全的写入

Bri*_*Yeh 3 python mongodb pymongo

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

update_one() got an unexpected keyword argument 'w'
Run Code Online (Sandbox Code Playgroud)

这样做的正确方法是什么?我看到插入接受'w'但不接受update_one或update_many.为什么?

A. *_*vis 6

覆盖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}})
Run Code Online (Sandbox Code Playgroud)

有关写入问题with_options,请参阅文档