📖 Scenario: You are setting up a MongoDB database for a small company. You need to create users with specific roles to control access to the database.
🎯 Goal: Learn how to create users and assign roles in MongoDB to manage database access securely.
📋 What You'll Learn
Create a user with read-only access to a database
Create a user with readWrite access to a database
Create a custom role with specific privileges
Assign the custom role to a new user
💡 Why This Matters
🌍 Real World
In real companies, controlling who can read or write data is important for security and data integrity.
💼 Career
Database administrators and backend developers often create users and roles to manage access in production databases.
Progress0 / 4 steps
1
Create a read-only user
Use the db.createUser() method to create a user called readonlyUser with password readonlyPass123 who has the read role on the companyDB database.
MongoDB
Hint
Remember to specify the user name, password, and roles array with the correct role and database.
2
Create a readWrite user
Create a user called writerUser with password writePass456 who has the readWrite role on the companyDB database using db.createUser().
MongoDB
Hint
Use the same method as before but change the user name, password, and role to readWrite.
3
Create a custom role
Use db.createRole() to create a role called customReporter on the companyDB database. This role should have the privilege to find documents on the reports collection.
MongoDB
Hint
Define the role name, privileges with resource and actions, and an empty roles array.
4
Assign the custom role to a new user
Create a user called reportUser with password reportPass789 who has the customReporter role on the companyDB database using db.createUser().
MongoDB
Hint
Use db.createUser with the new user name, password, and assign the customReporter role.
Practice
(1/5)
1. What is the primary purpose of creating users in MongoDB?
easy
A. To control who can access the database
B. To store data in collections
C. To optimize query performance
D. To backup the database automatically
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 A
Quick Check:
Users = Access control [OK]
Hint: Users manage access, not data or backups [OK]
Common Mistakes:
Confusing users with data storage
Thinking users optimize queries
Assuming users handle backups
2. Which of the following is the correct syntax to create a user with read-only access on the sales database in MongoDB?
easy
A. db.createUser({user: 'reader', pwd: 'pass', roles: [{role: 'read', db: 'sales'}]})
B. db.createUser({user: 'reader', pwd: 'pass', roles: ['read']})
C. db.addUser('reader', 'pass', 'read')
D. db.users.insert({user: 'reader', pwd: 'pass', role: 'read'})
Solution
Step 1: Recall MongoDB user creation syntax
The correct syntax uses db.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.
C. Roles must be objects specifying role and database
D. Missing database name in createUser command
Solution
Step 1: Check roles format in createUser
Roles must be an array of objects with role and db fields, 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 C
Quick Check:
Roles = [{role: '', db: ''}] [OK]
Hint: Roles need role and db objects, not strings [OK]
Common Mistakes:
Using role names as plain strings
Ignoring role database specification
Assuming password length causes failure
5. You want to create a custom role named reportViewer that allows users to only read data from the reports collection in the analytics database. Which command correctly creates this role?