db.grantRolesToUser() in mongoDB

grantRolesToUser(username, roles, writeConcern?)

This method grants additional roles to a user.


Syntax:

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


Example:

use admin

db.grantRolesToUser(

  "dbUser",

  [ "readWrite" , { role: "read", db: "stock" } ],

  { w: "majority" , wtimeout: 4000 }

)

MongoDB


db.grantRolesToUser(    
"dbUser",    
[ "readWrite" , { role: "read", db: "stock" } ],
 
{ w: "majority" , wtimeout: 4000 }  
)




To check the grant roles use the below command

 db.getUser(userName)

MongoDB

db.getUser("dbUser")

Output:

{
    "_id" : "admin.dbUser",
    "userId" : UUID("a43d6942-5385-49fc-a452-8183dbefa2f3"),
    "user" : "dbUser",
    "db" : "admin",
    "roles" : [
            {
                    "role" : "read",
                    "db" : "stock"
            },
            {
                    "role" : "readWrite",
                    "db" : "admin"
            },
            {
                    "role" : "dbAdmin",
                    "db" : "admin"
            }
    ],
    "mechanisms" : [
            "SCRAM-SHA-1",
            "SCRAM-SHA-256"
    ]
}


No comments:

Post a Comment