0
0
MongoDBquery~30 mins

Custom role creation in MongoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
Create a Custom Role in MongoDB
📖 Scenario: You are a database administrator for a small company. You need to create a custom role in MongoDB that allows users to read data from the sales database and insert documents into the feedback collection within the customer database.
🎯 Goal: Build a MongoDB custom role named readSalesInsertFeedback with the exact privileges to read from the sales database and insert into the feedback collection of the customer database.
📋 What You'll Learn
Create a role named readSalesInsertFeedback
Grant find action on all collections in the sales database
Grant insert action on the feedback collection in the customer database
Use the db.createRole() method with the correct structure
💡 Why This Matters
🌍 Real World
Custom roles help control exactly what users can do in a MongoDB database, improving security and organization.
💼 Career
Database administrators and developers often create custom roles to tailor access permissions to business needs.
Progress0 / 4 steps
1
Setup Role Name and Empty Privileges
Create a variable called roleDefinition and assign it an object with the role set to "readSalesInsertFeedback" and an empty array for privileges. Also include roles as an empty array.
MongoDB
Need a hint?

Start by defining the role name and empty privileges and roles arrays.

2
Add Read Privilege on Sales Database
Add an object to roleDefinition.privileges granting the find action on the resource with db set to "sales" and collection set to an empty string (meaning all collections).
MongoDB
Need a hint?

Use collection: "" to specify all collections in the sales database.

3
Add Insert Privilege on Feedback Collection
Add another object to roleDefinition.privileges granting the insert action on the resource with db set to "customer" and collection set to "feedback".
MongoDB
Need a hint?

Specify the exact collection feedback in the customer database for the insert action.

4
Create the Role in MongoDB
Use db.createRole() with roleDefinition as the argument to create the custom role.
MongoDB
Need a hint?

Call db.createRole() with the role definition object to create the role.