from pymongo import MongoClient
uri = "mongodb://localhost:27017" # Replace with your MongoDB connection URI
client = MongoClient(uri)
db_name = "your_database_name" # Replace with your database name
db = client[db_name]
current_write_concern = db.command("getLastError", 1)["w"]
print(f"Current write concern: {current_write_concern}")
if current_write_concern != 2:
print("Updating write concern to 2...")
try:
admin_db = client["admin"]
response = admin_db.command("setParameter", 1, defaultWriteConcern={"w": 2})
print("Write concern updated:", response)
except Exception as error:
print("Failed to update write concern:", error)
else:
print("Write concern is already 2.")
client.close()