Saturday, 22 May 2021

How to create the user and assign multiple roles for Single Database in MongoDB? | MongoDB Tutorial

🚀 Master MongoDB with Ram N Java!

If you found this helpful, join our growing community for more easy-to-follow tech guides.

🔴 SUBSCRIBE TO OUR CHANNEL

Assigning Multiple Roles to a Single User in MongoDB

In a real-world database environment, a user often needs to perform more than just one type of task. Instead of creating multiple accounts for one person, MongoDB allows you to assign multiple roles to a single user profile. This keeps your security organized and efficient!

Why Use Multiple Roles?

Think of roles like "keys" on a keychain. One key might open the front door (read access), while another opens the filing cabinet (dbAdmin access). By giving a user multiple roles, you are giving them the specific "keys" they need to do their job without giving them total control over everything.

How to Assign Multiple Roles

When you create or update a user, you define the roles in an array. Here is a simple example of how the command looks:

db.createUser({   user: "SpecialUser",   pwd: "securePassword123",   roles: [     { role: "readWrite", db: "marketing" },     { role: "dbAdmin", db: "marketing" }   ] })

Key Takeaways for Beginners

  • Array Format: Always use square brackets [ ] to list multiple roles.
  • Granular Control: You can mix and match roles like read, readWrite, and dbAdmin based on specific needs.
  • Database Specific: Remember to specify which database (db) each role applies to!

Beginner Tip

It is always better to start with the minimum roles needed and add more later. This is a security best practice called the "Principle of Least Privilege."

More Useful Tutorials from Our Channel:

No comments:

Post a Comment

Tutorials