MongoDB Create User and Assign Roles with Examples

By Sruthy

By Sruthy

Sruthy, with her 10+ years of experience, is a dynamic professional who seamlessly blends her creative soul with technical prowess. With a Technical Degree in Graphics Design and Communications and a Bachelor’s Degree in Electronics and Communication, she brings a unique combination of artistic flair…

Learn about our editorial policies.
Updated March 7, 2024

How to Create User, Add User, Create Admin User and Assign Roles in MongoDB:

MongoDB Database Profiler was explained in detail in our previous tutorial in this MongoDB Training Series for All.

In this tutorial, we will learn about User creation and assigning roles in MongoDB.

12. User Creation and Assigning Roles

Create User in MongoDB

MongoDB allows us to create a new user within the system in a very efficient way. If the user which we are going to insert already exists, then it will return an error in response. If there is no user that already exists then insert that record within the system.

Syntax

db.createUser(user, writeConcern)
  • user – It is the type of document and contains authentication regarding the user information which you want to insert within the system.
  • writeConcern – It is the level of permission which is used to write something on physical memory.

The user contains the following fields:

  • user – The user name.
  • pwd – The user password.
  • customData – The user associative information.
  • roles – The access level or privilege level of a user.
  • authenticationRestrictions – Auth permissions for a user.

Access Required

  • To create a new user within the system, you must have permission to create a new user.
  • To assign the roles within the system you must have grant role permission.

Code

 use products
db.createUser( { user: " AdminSTH",
pwd: " AdminSTH",
customData: { employeeId: 12345 },
roles: [ { role: "clusterAdmin", db: "admin" },
{ role: "readAnyDatabase", db: "admin" },
"readWrite"] },
{ w: "majority" , wtimeout: 5000 } )

The operation gives the following roles to AdminSTH:

  • The admin database clusterAdmin and readAnyDatabase roles.
  • The readWrite role in the product database.

Create Role in MongoDB

We can create an independent role within MongoDB by expressing permissions on all privileges explicitly. We can also inherit privileges from a different role in the new roles.

Syntax

db.createRole(role, writeConcern)

  • role – Information and permissions regarding the role.
  • writeConcern – It is the level of permission which is used to write something on physical memory.

The role contains the following fields:

  • role – The role name.
  • privileges – List of permissions.
  • roles – The access level or privilege level of a user.
  • authenticationRestrictions – Auth permissions for a user.

Access Required

  • In order to create a new user within the system, you must have permission to create a new user.
  • To assign roles within the system you must have grant role permission.

Code

 use admin
db.createRole(
{
role: "myClusterAdmin",
privileges: [
{ resource: { cluster: true }, actions: [ "addShard" ] },
{ resource: { db: "config", collection: "" }, actions: [ "find", "update", "insert", "remove" ] },
{ resource: { db: "users", collection: "usersCollection" }, actions: [ "update", "insert", "remove" ] },
{ resource: { db: "", collection: "" }, actions: [ "find" ] }
],
roles: [
{ role: "read", db: "admin" }
]
},
{ w: "majority" , wtimeout: 5000 }
)

Conclusion

There are two major things which have been discussed in this tutorial. In the first phase, we have covered how we can create a user in MongoDB and all the required things which we need to create that.

We have also covered how to assign a specific role to a user during creation.

In the second phase, we learned how we can create a role which has custom privileges. You can copy paste the available code in MongoDB and Robo 3T to create the user, assign role and role creation.

Check out our upcoming tutorial to know more about Authentication in MongoDB.

PREV Tutorial | NEXT Tutorial

Was this helpful?

Thanks for your feedback!

Leave a Comment