MongoDB Grant Roles To User

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

In this tutorials we will learn how to grant roles to user. db.grantRolesToUser() - method grants additional roles to a user account. Refer following db.grantRolesToUser() method syntax.

tags mongodb

db.grantRolesToUser() syntax

db.grantRolesToUser( "<username>", [ <roles> ], { <writeConcern> } )

user - string type parameter, The name of the user to whom to grant roles.

roles - array type parameter, An array of additional roles to grant to the user

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

Print existing user details

You can use MongoDB db.getUsers() command to print existing users or use db.getUser("uername") command to get specific user details.

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

Grant New role to user account

In below example we will add new role "read" to "scott" user in the "technicalkeeda" database.

use technicalkeeda
db.grantRolesToUser(
   "scott",
   [ "readWrite" , { role: "read", db: "technicalkeeda" } ],
   { w: "majority" , wtimeout: 4000 }
)

Print user details

> db.getUsers()
[
        {
                "_id" : "technicalkeeda.scott",
                "user" : "scott",
                "db" : "technicalkeeda",
                "customData" : {
                        "employeeId" : 999,
                        "employeeName" : "Yashwant Chavan",
                        "dept" : "Information Technology"
                },
                "roles" : [
                        {
                                "role" : "read",
                                "db" : "technicalkeeda"
                        },
                        {
                                "role" : "readWrite",
                                "db" : "technicalkeeda"
                        },
                        {
                                "role" : "dbAdmin",
                                "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]