Users in MongoDB are created to control who can access the database and what actions they can perform. This helps keep data safe and organized.
Click to reveal answer
beginner
What is a role in MongoDB?
A role is a set of permissions that define what actions a user can do in the database, like reading data or writing data.
Click to reveal answer
intermediate
How do you create a user with read-only access to a database in MongoDB?
You use the command db.createUser() with the role 'read' on the specific database. For example: db.createUser({user: 'reader', pwd: 'password', roles: [{role: 'read', db: 'mydb'}]})
Click to reveal answer
intermediate
What command is used to create a custom role in MongoDB?
Use db.createRole() with a role name and a list of privileges. For example: db.createRole({role: 'myRole', privileges: [{resource: {db: 'mydb', collection: ''}, actions: ['find']}], roles: []})
Click to reveal answer
beginner
Why is it important to assign roles instead of giving all privileges to a user?
Assigning specific roles limits what a user can do, which improves security by preventing accidental or malicious changes to the database.
Click to reveal answer
Which MongoDB command is used to add a new user?
Adb.insertUser()
Bdb.createUser()
Cdb.addUser()
Ddb.newUser()
✗ Incorrect
The correct command to create a user in MongoDB is db.createUser().
What does a role in MongoDB define?
AThe user's password
BThe user's email
CThe user's permissions
DThe user's database name
✗ Incorrect
A role defines the permissions or actions a user can perform in the database.
Which role would you assign to a user who should only read data?
Aroot
BdbAdmin
CreadWrite
Dread
✗ Incorrect
The 'read' role allows a user to only read data without making changes.
What is the purpose of db.createRole()?
ATo create a custom role with specific privileges
BTo create a new user
CTo create a new database
DTo delete a role
✗ Incorrect
db.createRole() is used to create a custom role with specific permissions.
Why should you avoid giving users the 'root' role unless necessary?
AIt gives full access which can be risky
BIt only allows reading data
CIt disables their account
DIt limits their access
✗ Incorrect
The 'root' role gives full access to the database, which can be risky if misused.
Explain how to create a user with read and write access to a specific database in MongoDB.
Think about the command and the role that allows both reading and writing.
You got /4 concepts.
Describe the steps to create a custom role that allows only reading from a specific collection.
Focus on defining privileges and then assigning the role.
You got /4 concepts.
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?