7

There is a user in database, this user should be renamed. How to rename the user? The MongoDB user management reference has method db.updateUser but I don't see how to set a new name for the user. How to update the username? ty

db.updateUser(
   "<username>",
   {
     customData : { <any information> },
     roles : [
               { role: "<role>", db: "<database>" } | "<role>",
               ...
             ],
     pwd: "<cleartext password>"
    },
    writeConcern: { <write concern> }
)

1 Answer 1

12

Did you try to update the user?

db.system.users.update({"user":"oldname"}, {$set:{"user":"newname"}})

This command requires root access to admin database.

7
  • This way didn't try. Is your approach common why then there's db.updateUser() supported? Anyway thank you for the solution :). Feb 8, 2016 at 9:51
  • Well, off the top of my head updateUser encrypts plain passwords, and don't require access to system.users database.
    – Alex Blex
    Feb 8, 2016 at 9:58
  • 6
    btw the command db.system.users.update({"user":"oldname"}, {$set:{"user":"newname"}}) gave error ("errmsg" : "not authorized on admin to execute command { update:) for the user with following roles roles : [ { role : "userAdminAnyDatabase", db : "admin" } ]. Had to auth as a user that has roles roles : [ { role : "root", db : "admin" } ] to successfully issue db,system.users.update(.... Feb 8, 2016 at 9:59
  • Updated the answer. Thanks.
    – Alex Blex
    Feb 8, 2016 at 10:12
  • @Willmore Alex is right, the updateUser is one of the many shell convenience method, as can be seen when issuing a db.updateUser (note the missing parenthesis. I would like to add that you don't necessarily have to be root. Any User with the role userAdmin for the authenticationDatabase (admin in this case) or userAdminAnyDatabase can do this. Be careful with the latter, the user having this role can grant any privilege to himself or others. Feb 8, 2016 at 11:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.