Ticket: Durable Writes: majority

Hello,

I have seen many examples how to implement Write Concern, (for example https://programtalk.com/python-examples/pymongo.write_concern.WriteConcern/) but with integers. (w=0, w=2, etc)
How can you specify majority in Python code?

I tried
collection = db.users
majority = WriteConcern(‘majority’)
w1_collection = collection.with_options(write_concern=WriteConcern(w=majority ))
w1_collection.insert_one({“name”: name, “email”: email, “password”: hashedpw })

Thanks.

1 Like

Gozer :slight_smile:

It took me quite some puzzling and Googling… The right format turns out to be quite different.

userscoll = db.users.with_options(write_concern=WriteConcern("majority"))
userscoll.insert_one({"name":name,"email":email,"password":hashedpw})

Basically, we first specify the collection, with added options. Only afterwards do we insert into it.

3 Likes

Nice one, tried and tested - working.
Thanks / dank je wel :grinning: !

1 Like