Built-in roles (read, readWrite, dbAdmin) in MongoDB - Time & Space Complexity
When using built-in roles in MongoDB, it's important to understand how the time to check permissions grows as the number of operations increases.
We want to know how the system handles permission checks as more database actions happen.
Analyze the time complexity of permission checks using built-in roles.
// Example: Checking user permissions for a read operation
const user = db.getUser("alice");
if (user.roles.some(role => role.role === "read")) {
db.collection.find({});
}
// Roles: read, readWrite, dbAdmin
// Each role grants different access levels
This code checks if a user has the 'read' role before allowing a read operation on a collection.
Look at what repeats when checking permissions.
- Primary operation: Checking if the user's roles include the required role.
- How many times: Once per operation that requires permission.
The number of roles a user has is usually small and fixed, so checking roles stays quick even if many operations happen.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 operations | 10 role checks |
| 100 operations | 100 role checks |
| 1000 operations | 1000 role checks |
Pattern observation: The time grows linearly with the number of operations, but each role check is very fast because roles list is short.
Time Complexity: O(n)
This means the total time to check permissions grows directly with the number of operations performed.
[X] Wrong: "Checking roles takes a long time because the database is big."
[OK] Correct: Role checks depend on the user's roles, which are few, not on the database size.
Understanding how permission checks scale helps you explain security and performance in real applications confidently.
"What if a user had hundreds of roles assigned? How would that affect the time complexity of permission checks?"
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
