Creating users and roles in MongoDB - Performance & Efficiency
When we create users and roles in MongoDB, we want to know how the time it takes changes as we add more users or roles.
We ask: How does the work grow when the number of users or roles grows?
Analyze the time complexity of the following code snippet.
db.createUser({
user: "alice",
pwd: "password123",
roles: [ { role: "readWrite", db: "sales" } ]
});
db.createRole({
role: "reporter",
privileges: [ { resource: { db: "sales", collection: "reports" }, actions: [ "find" ] } ],
roles: []
});
This code creates one user and one role with specific permissions in the database.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Indexed lookup to check existing users and roles to ensure no duplicates before creation.
- How many times: This check happens once per user or role creation.
MongoDB uses indexes to perform a fast lookup for duplicates, regardless of the number of existing users or roles.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 indexed lookup |
| 100 | 1 indexed lookup |
| 1000 | 1 indexed lookup |
Pattern observation: The work remains constant as the number of users or roles already in the system grows.
Time Complexity: O(log n)
This means the time to create a user or role grows logarithmically with the number of existing users or roles, which is effectively very fast and close to constant time for practical purposes.
[X] Wrong: "Creating a user or role takes longer as more exist because it scans all entries."
[OK] Correct: MongoDB uses indexes on usernames and rolenames for fast O(log n) lookups, which is effectively constant time.
Understanding how user and role creation scales (logarithmic time due to indexing) helps you explain system behavior clearly and shows you think about real database operations.
"What if we batch create 100 users at once? How would the time complexity change?"
Practice
Solution
Step 1: Understand user creation in MongoDB
Users in MongoDB are created to manage access to the database system.Step 2: Identify the purpose of users
Users control who can connect and perform actions on the database.Final Answer:
To control who can access the database -> Option AQuick Check:
Users = Access control [OK]
- Confusing users with data storage
- Thinking users optimize queries
- Assuming users handle backups
sales database in MongoDB?Solution
Step 1: Recall MongoDB user creation syntax
The correct syntax usesdb.createUser()with roles as an array of objects specifying role and database.Step 2: Check each option
db.createUser({user: 'reader', pwd: 'pass', roles: [{role: 'read', db: 'sales'}]}) correctly specifies the role 'read' on the 'sales' database. db.createUser({user: 'reader', pwd: 'pass', roles: ['read']}) misses the database name, C uses deprecated syntax, D tries to insert directly into users collection which is invalid.Final Answer:
db.createUser({user: 'reader', pwd: 'pass', roles: [{role: 'read', db: 'sales'}]}) -> Option AQuick Check:
Roles need role and db fields [OK]
- Omitting the database in roles
- Using deprecated addUser method
- Trying to insert user directly into collection
db.createUser({user: 'adminUser', pwd: 'secure123', roles: [{role: 'userAdminAnyDatabase', db: 'admin'}]})What permission does the user
adminUser have?Solution
Step 1: Understand the role
This role allows managing users and roles across all databases.userAdminAnyDatabaseStep 2: Analyze the permissions given
The user can create, modify, and delete users on any database but does not have read or write data permissions by default.Final Answer:
Can manage users on any database -> Option BQuick Check:
userAdminAnyDatabase = user management [OK]
- Assuming it grants read or write data access
- Confusing admin database with all databases
- Thinking it only applies to admin database
db.createUser({user: 'testUser', pwd: 'test123', roles: ['readWrite']})But it fails. What is the most likely reason?
Solution
Step 1: Check roles format in createUser
Roles must be an array of objects withroleanddbfields, not just strings.Step 2: Identify the error cause
The command uses roles as strings, which is invalid syntax causing failure.Final Answer:
Roles must be objects specifying role and database -> Option CQuick Check:
Roles = [{role: '', db: ''}] [OK]
- Using role names as plain strings
- Ignoring role database specification
- Assuming password length causes failure
reportViewer that allows users to only read data from the reports collection in the analytics database. Which command correctly creates this role?Solution
Step 1: Understand custom role creation syntax
Custom roles require specifying privileges with resource (db and collection) and actions allowed.Step 2: Analyze each option's privileges
db.createRole({role: 'reportViewer', privileges: [{resource: {db: 'analytics', collection: 'reports'}, actions: ['find']}], roles: []}) correctly specifies the database 'analytics', collection 'reports', and action 'find' which allows reading documents. db.createRole({role: 'reportViewer', privileges: [{resource: {db: 'analytics'}, actions: ['read']}], roles: []}) uses 'read' action on the whole database, not just the collection. db.createRole({role: 'reportViewer', privileges: [{resource: {collection: 'reports'}, actions: ['find']}], roles: []}) misses the database name. db.createRole({role: 'reportViewer', privileges: [{resource: {db: 'analytics', collection: 'reports'}, actions: ['read']}], roles: []}) uses 'read' action which is not a valid action for privileges; 'find' is correct for reading documents.Final Answer:
db.createRole({role: 'reportViewer', privileges: [{resource: {db: 'analytics', collection: 'reports'}, actions: ['find']}], roles: []}) -> Option DQuick Check:
Custom role needs db, collection, and 'find' action [OK]
- Using 'read' action instead of 'find'
- Omitting database name in resource
- Specifying collection without database
Ready to prove it?
Pass the test and this pattern moves unknown
