0

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

Dumb
  • 83
  • 1
  • 10
  • Check out [How to run raw mongodb commands from pymongo](https://stackoverflow.com/q/27297727/2282634), and [grantRolesToUser](https://docs.mongodb.com/manual/reference/command/grantRolesToUser/#grantrolestouser) – Joe May 12 '21 at 08:17

1 Answers1

2

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"])
  • I used as above but got the below error- pymongo.errors.operationFailure: "user" is not a valid argument to grantRolesToUser, full error: {'ok': 0.0, 'errmsg': '"user" is not a valid argument to grantRolesToUser'. 'code': 2, 'codeName': 'BadValue'} – Dumb May 12 '21 at 09:48
  • I removed the keyword user and it worked db.command("grantRolesToUser", "your_username", roles=["read"])) – Dumb May 12 '21 at 09:53
  • My bad, I edit the answer. Please mark the answer as accepted if it helps you. – Zasda Yusuf Mikail May 12 '21 at 09:56