2015-06-10 61 views
2

我的連接默認爲w = 0 ,但對於collection.update_one或collection.update_many,我希望通過設置參數來設置每個操作的write_concern W = 0。相反,我得到這個錯誤:如何使用pymongo的collection.update_one或update_many指定不安全/安全的寫入

update_one() got an unexpected keyword argument 'w' 

什麼是正確的方法來做到這一點?我看到insert接受'w',但不接受update_one或update_many。爲什麼?

+0

我也面臨着同樣的問題,並添加如果你想在update_one或update_many操作中明確指定write_concern,那麼這裏是pull請求https://github.com/mongodb/mongo-python-driver/pull/313 –

回答

4

來覆蓋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 concernwith_options