from pymongo import MongoClient from pymongo import monitoring class CommandLogger(monitoring.CommandListener): def started(self, event): print("Command {0.command_name} with request id " "{0.request_id} started on server " "{0.connection_id[1]}\n" "command {0.command}\n".format(event)) def succeeded(self, event): print("Command {0.command_name} with request id " "{0.request_id} on server {0.connection_id[1]} " "succeeded in {0.duration_micros} " "microseconds\n" "reply {0.reply}\n".format(event)) def failed(self, event): print("Command {0.command_name} with request id " "{0.request_id} on server {0.connection_id[1]} " "failed in {0.duration_micros} " "microseconds\n" "reply {0.failure}\n".format(event)) monitoring.register(CommandLogger()) #update connection string: hostname, db, repl_set_name client = MongoClient("mongodb://hostname/db?replicaSet=repl_set_name&w=majority&journal=true&retryWrites=true") db = client.get_database() coll = db.update_test db.drop_collection("update_test") #rslt = coll.insert_one({"_id": 1, "sample": "text"}) rslt = coll.insert_many([{"_id": 1, "sample": "text"}, {"_id": 2, "sample": "some stuff"}]) ready = input("\n Shut down the Primary member, Ready? (y/n) ") if len(ready) > 1 or ready != 'y': raise SystemExit rslt = coll.update_one({"_id": 2}, {"$set": {"test_key": "test_value"}})