What if you could stop worrying about who can do what in your database with just a few simple commands?
Why Built-in roles (read, readWrite, dbAdmin) in MongoDB? - Purpose & Use Cases
Imagine you have a big library of books (your database) and many people want to use it. You try to keep track of who can only read books, who can add or change books, and who can manage the whole library by writing notes on paper.
Writing down permissions on paper is slow and confusing. You might forget who can do what, accidentally give too much power, or lose track of changes. This can cause mistakes and security problems.
Built-in roles in MongoDB are like ready-made badges that you give to people. Each badge clearly says what they can do: just read, read and write, or manage the database. This makes managing access easy, safe, and fast.
Keep a notebook listing users and their permissions; update manually for each change.
db.grantRolesToUser('alice', [{ role: 'readWrite', db: 'mydb' }])
With built-in roles, you can quickly and safely control who can see, change, or manage your data without confusion or mistakes.
A company lets its sales team only read customer data, the support team read and update tickets, and the IT team manage the database settings--all using built-in roles.
Manual permission tracking is slow and error-prone.
Built-in roles provide clear, ready-to-use access levels.
They make database security simple and reliable.
Practice
Solution
Step 1: Understand the
Thereadrolereadrole allows users to view data but not modify it.Step 2: Compare with other roles
readWriteallows changes,dbAdminmanages settings,roothas full access.Final Answer:
read-> Option DQuick Check:
View-only permission =read[OK]
read role [OK]- Confusing
readWritewithread - Thinking
dbAdminallows data viewing only - Assuming
rootis for read-only
readWrite role to a user on the database mydb in MongoDB shell?Solution
Step 1: Check syntax for granting roles
The correct syntax usesdb.grantRolesToUserwith an array of role objects specifying role and database.Step 2: Validate database name and role format
db.grantRolesToUser('username', [{role: 'readWrite', db: 'mydb'}]) correctly specifies role as 'readWrite' and database as 'mydb'. db.grantRolesToUser('username', [{role: 'readWrite', db: 'admin'}]) wrongly uses 'admin' database.Final Answer:
db.grantRolesToUser('username', [{role: 'readWrite', db: 'mydb'}]) -> Option AQuick Check:
Grant role with role+db object array [OK]
- Passing roles as simple strings without db
- Using wrong database name for the role
- Using createUser instead of grantRolesToUser for existing users
dbAdmin role on database sales, which of the following actions can the user perform?Solution
Step 1: Understand
ThedbAdminrole capabilitiesdbAdminrole allows managing database settings like indexes, stats, and users, but not data changes.Step 2: Match actions to role permissions
Creating/dropping indexes is a dbAdmin task. Inserting, reading, or modifying documents requirereadWriteorreadroles.Final Answer:
Create and drop indexes on collections -> Option AQuick Check:
dbAdmin manages indexes, not data changes [OK]
- Assuming dbAdmin can insert or modify data
- Confusing readWrite with dbAdmin
- Thinking dbAdmin can read data
readWrite role to a user with this command:db.grantRolesToUser('alice', ['readWrite'])But it failed. What is the likely cause?
Solution
Step 1: Check syntax for grantRolesToUser
The method requires an array of role objects withroleanddbfields, not just strings.Step 2: Analyze the given command
The command uses an array of strings, which is invalid syntax and causes failure.Final Answer:
The role array must contain objects with role and db fields -> Option BQuick Check:
Roles must be objects with role and db [OK]
- Passing roles as strings instead of objects
- Assuming createUser is needed to add roles to existing users
- Ignoring user existence errors
Solution
Step 1: Identify roles for viewing data and managing indexes
readallows viewing data,dbAdminallows managing indexes and database settings.Step 2: Exclude roles that allow modifying documents
readWriteallows modifying documents, which is not desired here.Final Answer:
readanddbAdmin-> Option CQuick Check:
View + manage indexes = read + dbAdmin [OK]
- Using readWrite which allows document changes
- Assigning only dbAdmin which lacks read permission
- Assuming dbAdmin includes readWrite
Ready to prove it?
Pass the test and this pattern moves unknown
