I have a pymongo collection and its write concern options are following:
>>> col1.__class__.__bases__
(<class 'pymongo.collection.Collection'>,)
>>> col1.write_concern
{'wtimeout': 6000}
I'm trying to insert a document in a replica set with only master available (slaves are blocked)
>>> pymongo.collection.Collection.update(col1, {'_id': '11'}, { "_id" : "11", "key": "test" }, upsert=True, fsync=False, w=2)
And pymongo hangs forever. Seems like setting w=2 explicitly overrides default wtimeout value to 0. If I send wtimeout=6000 to update it will throw an exception as expected. Am I missing something, or is it supposed to work this way?
It's true that if you pass any write concern options to "update", they replace all the default write concern options. I don't think we've documented that.
Regardless, "update" is deprecated in favor of the much clearer update_one and update_many methods:
>>> from pymongo import WriteConcern
>>> collection = MongoClient().db.collection
>>> coll2 = collection.with_options(
... write_concern=WriteConcern(w=2, wtimeout=6000))
>>> oid = coll2.replace_one({'_id': '11'}, { "_id" : "11", "key": "test" },
upsert=True)
See the PyMongo 3 guide:
Also, I'd recommend you use Python objects in the typical way, as in my example: instantiate an object and call a method on the instance, instead of calling a method on a class and passing an instance as "self".