I'm trying to write a Python script to add a user to MongoDB using pymongo 2.8. Here is my code:
db = conn['test123']
collection=db['testing']
db1 = conn['admin']
try:
data = db1.add_user('test123user', 'test123pass', roles=[{'role':'readWrite','db':'test123'}])
print("Database with user is created")
except Exception as e:
print("error: Unable to create the user for database ")
raise
When I run the same code multiple times I don't see any error like we see on mongo shell when we insert a duplicate user:
2017-01-06T17:29:59.209-0500 E QUERY [thread1] Error: couldn't add user: User "db09user@admin" already exists
How can I avoid inserting duplicate users through pymomgo? Thanks!