pythonpython-3.xmongodbpymongopymongo-3.x

add roles to a user in mongodb via python (pymongo)


I need help in adding new roles to an existing user in mongo DB. I need the solution in Pymongo. I would appreciate your help

I tried using db._create_or_update_user() db._create_or_update_user( "UserName","password", roles= [ {"role": "read", "db": "databbase"} ] )

But I am getting the error, Missing 2 required positional arguments, password and readonly


Solution

  • You can use "raw" mongodb command with pymongo. Like this:

    from pymongo import MongoClient
    
    client = MongoClient()
    
    db = client.your_database_name
    
    db.command("grantRolesToUser", "your_username", roles=["read"])