What if one simple step could stop accidental data leaks and keep your database safe?
Creating users and roles in MongoDB - Why You Should Know This
Imagine you have a big library where many people want to borrow books. You write down who can borrow which books on paper. Every time someone new comes, you add their name and what they can borrow manually.
Writing and updating permissions on paper is slow and confusing. You might forget to update someone's access or give too many rights by mistake. This can cause chaos and mistakes that are hard to fix.
Creating users and roles in MongoDB lets you manage who can do what easily and safely. You define roles once and assign them to users, so permissions are clear and controlled automatically.
Write names and permissions on paper; update manually for each user.
db.createUser({user: 'alice', pwd: 'pass123', roles: ['readWrite']})You can safely control access to your database, making sure each user has just the right permissions without extra hassle.
A company database where developers can read and write data, but only admins can change user roles or delete data.
Manual permission tracking is slow and error-prone.
Users and roles in MongoDB automate and secure access control.
This keeps your data safe and your team productive.
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
