0

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!

Tagc
  • 7,701
  • 6
  • 47
  • 99
monicak
  • 45
  • 3

1 Answers1

0

According to the pymongo docs

Will change the password if user name already exists.

It means you are not creating a new user if the user already created, with this script. So there is no duplicate problem.

metmirr
  • 3,809
  • 2
  • 18
  • 33