MongoDB Update User

By Yashwant Chavan, Views 10048, Last updated on 01-Mar-2019

In this tutorial, we will learn how to update an existing user for the database using MongoDB db.updateUser() method.

tags mongodb

mongoDB Update User - db.updateUser()

db.updateUser() method is used to update existing user details for the database. An update fields completely replaces the previous fields values, When you updates the roles array it also replace the user's existing roles. So be careful while updating user's details.

db.updateUser(username, update, writeConcern)

username - string contains name of the user to update.

update - document contains the user data which you want to update, including customData , roles and password.

writeConcern - Optional document parameter , Introduce in version 2.6. It describe level of acknowledgement requested from MongoDB while performing write / create operations to a standalone mongoDB, replica sets, sharded clusters.

update document syntax

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

customData - document type optional parameter, Any arbitrary information about user like user full name , employee id or department etc.

roles - array type parameter, which helps to specify the user roles. You can specify built-in roles or user-defined roles. If you don't want to specify user roles then pass an empty array [] to create users method.

pwd - String type parameter, The user’s password.

db.updateUser() Example

In below example we have existing user ("scott") for the database "technicalkeeda" along with its user credentials, custom data information and roles. Here We will update the user details like customData, roles and pwd. User document details are replaced with new field values, Refer below example.

Earlier user "scott" has two roles (readWrite,dbAdmin) and after updateUser one of dbAdmin role is removed.

> use technicalkeeda
switched to db technicalkeeda

> db.getUsers()
[
        {
                "_id" : "technicalkeeda.scott",
                "user" : "scott",
                "db" : "technicalkeeda",
                "customData" : {
                        "employeeId" : 9999,
                        "employeeName" : "Yashwant Chavan",
                        "dept" : "Information Technology"
                },
                "roles" : [
                        {
                                "role" : "readWrite",
                                "db" : "technicalkeeda"
                        },
                        {
                                "role" : "dbAdmin",
                                "db" : "technicalkeeda"
                        }
                ]
        }
]

> db.updateUser(
       "scott",
       {
         customData : {employeeId: 9999,employeeName: "Yashwant",dept: "IT"},
         roles: ["readWrite"],
         pwd: "newtiger"
        },
        {w: "majority",wtimeout: 5000}
)

> db.getUsers()
[
        {
                "_id" : "technicalkeeda.scott",
                "user" : "scott",
                "db" : "technicalkeeda",
                "customData" : {
                        "employeeId" : 9999,
                        "employeeName" : "Yashwant",
                        "dept" : "IT"
                },
                "roles" : [
                        {
                                "role" : "readWrite",
                                "db" : "technicalkeeda"
                        }
                ]
        }
]
>
Yashwant

Yashwant

Hi there! I am founder of technicalkeeda.com and programming enthusiast. My skills includes Java,J2EE, Spring Framework, Nodejs, PHP and lot more. If you have any idea that you would want me to develop? Lets connect: yashwantchavan[at][gmail.com]