Show/Hide Toolbars

MongoDB Notes

 

Add Roles to User

 

To add roles to a user, use this command:

 

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

 

Example

 

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

 

Remove Roles from User

 

To remove roles from a user, use this command:

 

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

 

Example

 

use products
db.revokeRolesFromUser( "accountUser01",
                        [ { role: "read", db: "stock" }, "readWrite" ],
                        { w: "majority" }
                      )

 

Related Commands

 

db.getUsers()

db.getUser(<username>)

db.createUser({user: 'steve', pwd: 'secret', roles: [{role: 'root', db: 'admin'}]})

db.auth(<username>, <password>)

 

References

 

Built-In Roles