Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

264
Views
Pymongo: setting w for collection update overrides default write concern options

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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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:

http://api.mongodb.com/python/current/migrate-to-pymongo3.html#the-write-concern-attribute-is-immutable

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".

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!