📖 Scenario: You are building a simple user management system for a small company. The system needs to control what actions users can perform based on their roles.For example, an admin can read and write all data, a manager can read and write some data, and a viewer can only read data.
🎯 Goal: Create a MongoDB collection to store users with their roles and permissions. Then, define a query to find users who have permission to write data.
📋 What You'll Learn
Create a MongoDB collection named users with documents containing username, role, and permissions fields.
Define roles with specific permissions: admin has read and write, manager has read and write, viewer has only read.
Write a query to find all users who have write permission.
💡 Why This Matters
🌍 Real World
Role-based access control is used in many applications to restrict what users can do based on their roles, improving security and organization.
💼 Career
Understanding how to manage user roles and permissions in databases is essential for backend developers, database administrators, and security engineers.
Progress0 / 4 steps
1
Create the users collection with user documents
Create a MongoDB collection called users and insert these exact documents: { username: "alice", role: "admin", permissions: ["read", "write"] }, { username: "bob", role: "manager", permissions: ["read", "write"] }, and { username: "carol", role: "viewer", permissions: ["read"] }.
MongoDB
Hint
Use db.users.insertMany() with an array of user objects.
2
Define a variable for the write permission
Create a variable called writePermission and set it to the string "write" to represent the write permission.
MongoDB
Hint
Use const writePermission = "write" to create the variable.
3
Write a query to find users with write permission
Write a MongoDB query called usersWithWrite that finds all documents in the users collection where the permissions array contains the value stored in writePermission.
MongoDB
Hint
Use db.users.find({ permissions: writePermission }) to find users with write permission.
4
Complete by adding a projection to show only usernames
Modify the usersWithWrite query to include a projection that returns only the username field and excludes the _id field.
MongoDB
Hint
Use the projection option in find() to select fields.
Practice
(1/5)
1. What is the main purpose of role-based access control (RBAC) in MongoDB?
easy
A. To optimize query performance automatically
B. To store data in a structured format
C. To manage user permissions by assigning roles with specific privileges
D. To backup the database regularly
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 C
Quick Check:
RBAC controls access by roles = A [OK]
Hint: RBAC controls user actions via roles, not data or backups [OK]
Common Mistakes:
Confusing RBAC with data storage
Thinking RBAC handles backups
Assuming RBAC optimizes queries
2. Which of the following is the correct syntax to create a role named readOnlyUser with read access on the sales database?
A. The field 'role' should be 'name' when creating a role
B. The 'actions' array cannot contain 'update'
C. The 'resource' field must include 'collection' as null, not a string
D. Roles cannot have empty 'roles' array
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 A
Quick Check:
Use 'name' field for role creation = B [OK]
Hint: Use 'name' not 'role' to name roles in createRole [OK]
Common Mistakes:
Using 'role' instead of 'name' for role name
Thinking 'update' is invalid action
Believing empty roles array is disallowed
5. You want to create a custom role 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?