w3resource

MongoDB: db.updateUser() method

db.updateUser()

The db.updateUser() method is used to update the user’s profile on the database on which you run the method. An update to a field completely replaces the previous field’s values. This includes updates to the user’s roles array.

Syntax:

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

Parameters:

Name Description Required /
Optional
Type
username The name of the user to update. Required string
update A document containing the replacement data for the user. This data completely replaces the corresponding data for the user. Required document
writeConcern The level of write concern for the update operation. The writeConcern document takes the same fields as the getLastError command. Optional document

The update document specifies the fields to update and their new values. All fields in the update document are optional, but must include at least one field.

The update document has the following fields:

Name Description Required /
Optional
Type
customData Any arbitrary information. Optional document
roles The roles granted to the user. An update to the roles array overrides the previous array’s values. Optional array
pwd The user’s password. Optional string

In the roles field, you can specify both built-in roles and user-defined role.

To specify a role that exists in the same database where db.updateUser() runs, you can either specify the role with the name of the role:

"readWrite"

Or you can specify the role with a document, as in:

{ role: "<role>", db: "<database>" }

To specify a role that exists in a different database, specify the role with a document.

Example: MongoDB: db.updateUser() method

Given a user mynewuser in the test database with the following user info:

> db.getUser("mynewuser");
{
        "_id" : "test.mynewuser",
        "user" : "mynewuser",
        "db" : "test",
        "roles" : [
                {
                        "role" : "readWrite",
                        "db" : "test"
                },
                {
                        "role" : "dbAdmin",
                        "db" : "test"
                }
        ]
}

he following db.updateUser() method completely replaces the user’s  roles data:

db.updateUser( "mynewuser",
               {
                 customData : { employeeId : "0x3039" },
                 roles : [
                           { role : "read", db : "assets"  }
                         ]
                }
             );

The user mynewuser  in the test database now has the following user information:

> db.getUser("mynewuser");
{
        "_id" : "test.mynewuser",
        "user" : "mynewuser",
        "db" : "test",
        "roles" : [
                {
                        "role" : "read",
                        "db" : "assets"
                }
        ],
        "customData" : {
                "employeeId" : "0x3039"
        }
}

Retrieve the restaurants data from here

Behavior

db.updateUser() sends password to the MongoDB instance without encryption.

Required Access

You must have access that includes the revokeRole action on all databases in order to update a user’s roles array.

You must have the grantRole action on a role’s database to add a role to a user.

To change another user’s pwd or customData field, you must have the changeAnyPassword and changeAnyCustomData actions respectively on that user’s database.

To modify your own password and custom data, you must have privileges that grant changeOwnPassword and changeOwnCustomData actions respectively on the user’s database.

Previous: db.createUser() method
Next: db.changeUserPassword() method



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/mongodb/shell-methods/user-management/db-updateUser.php