Role-based access control in MongoDB - Time & Space Complexity
When using role-based access control in MongoDB, we want to know how the time to check permissions changes as the number of roles or users grows.
We ask: How does the system handle more roles or users without slowing down too much?
Analyze the time complexity of the following MongoDB role check.
// Check if user has a role
const user = db.users.findOne({ username: "alice" });
const hasRole = user.roles.includes("readWrite");
if (hasRole) {
// allow action
} else {
// deny action
}
This code finds a user document and checks if the user has a specific role in their roles list.
Look for repeated steps that take time as data grows.
- Primary operation: Searching through the user's roles array to find a matching role.
- How many times: The check goes through each role in the array until it finds a match or finishes.
As the number of roles a user has increases, the time to check if a role exists grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 roles | Up to 10 checks |
| 100 roles | Up to 100 checks |
| 1000 roles | Up to 1000 checks |
Pattern observation: The time grows roughly in direct proportion to the number of roles checked.
Time Complexity: O(n)
This means the time to check a role grows linearly with the number of roles the user has.
[X] Wrong: "Checking a role is always instant no matter how many roles a user has."
[OK] Correct: The system must look through the roles list one by one, so more roles mean more time to check.
Understanding how role checks scale helps you design systems that stay fast as users and roles grow. This skill shows you can think about real-world system performance.
"What if roles were stored in a set or indexed structure instead of a list? How would the time complexity change?"
Practice
Solution
Step 1: Understand RBAC concept
RBAC is about controlling what users can do by giving them roles with permissions.Step 2: Identify RBAC purpose in MongoDB
MongoDB uses RBAC to assign roles that define user privileges, managing access securely.Final Answer:
To manage user permissions by assigning roles with specific privileges -> Option CQuick Check:
RBAC controls access by roles = A [OK]
- Confusing RBAC with data storage
- Thinking RBAC handles backups
- Assuming RBAC optimizes queries
readOnlyUser with read access on the sales database?Solution
Step 1: Check correct field for role name
The field to name the role isname, notrole.Step 2: Verify privileges for read access
Read access uses thefindaction on the database with empty collection string for all collections.Final Answer:
db.createRole({name: "readOnlyUser", privileges: [{resource: {db: "sales", collection: ""}, actions: ["find"]}], roles: []}) -> Option BQuick Check:
Role name uses 'name' field = A [OK]
- Using 'role' instead of 'name' for role creation
- Assigning wrong actions like 'insert' for read role
- Missing empty string for collection to cover all
db.runCommand({usersInfo: "alice"}) after assigning the role?db.createRole({
name: "reportViewer",
privileges: [{resource: {db: "reports", collection: ""}, actions: ["find"]}],
roles: []
})
db.createUser({
user: "alice",
pwd: "password123",
roles: ["reportViewer"]
})Solution
Step 1: Confirm role creation and assignment
The role 'reportViewer' is created with find privileges on 'reports' and assigned to user 'alice'.Step 2: Understand usersInfo command output
Running usersInfo for 'alice' shows user details including assigned roles and privileges.Final Answer:
Returns user info showing 'alice' has role 'reportViewer' with find privileges on 'reports' -> Option AQuick Check:
Role assigned correctly, usersInfo shows it = C [OK]
- Assuming usersInfo errors if role was just created
- Expecting no roles shown after assignment
- Confusing assigned roles with admin role
db.createRole({
role: "dataEditor",
privileges: [{resource: {db: "inventory", collection: "products"}, actions: ["update"]}],
roles: []
})But it failed. What is the error?
Solution
Step 1: Check role creation syntax
The correct field to name a role is 'name', not 'role'. Using 'role' causes failure.Step 2: Validate other fields
Actions like 'update' are valid, collection as string is correct, and empty roles array is allowed.Final Answer:
The field 'role' should be 'name' when creating a role -> Option AQuick Check:
Use 'name' field for role creation = B [OK]
- Using 'role' instead of 'name' for role name
- Thinking 'update' is invalid action
- Believing empty roles array is disallowed
manager that can read and update documents in the employees database and also inherit all privileges from the built-in read role on the same database. Which command correctly achieves this?Solution
Step 1: Check privileges for read and update on employees
Privileges should apply to all collections in 'employees' database, so collection is empty string "".Step 2: Verify inherited role database
The built-in 'read' role must be inherited from the same database 'employees', not 'admin'.Step 3: Confirm actions and roles
Actions 'find' and 'update' are correct; 'insert' is not required here.Final Answer:
db.createRole({name: "manager", privileges: [{resource: {db: "employees", collection: ""}, actions: ["find", "update"]}], roles: [{role: "read", db: "employees"}]}) -> Option DQuick Check:
Empty collection for all, inherit 'read' from same DB = D [OK]
- Setting collection name instead of empty string for all
- Inheriting 'read' role from wrong database
- Using wrong actions like 'insert' instead of 'find'
Ready to prove it?
Pass the test and this pattern moves unknown
