Custom roles let you control exactly what actions users can do in your database. This helps keep your data safe and organized.
Custom role creation in MongoDB
db.createRole({
role: "roleName",
privileges: [
{
resource: { db: "databaseName", collection: "collectionName" },
actions: ["action1", "action2"]
}
],
roles: []
})role: The name of your new role.
privileges: What the role can do and where.
db.createRole({
role: "readOnly",
privileges: [
{
resource: { db: "sales", collection: "orders" },
actions: ["find"]
}
],
roles: []
})db.createRole({
role: "orderManager",
privileges: [
{
resource: { db: "sales", collection: "orders" },
actions: ["find", "insert", "update", "remove"]
}
],
roles: []
})db.createRole({
role: "backupRole",
privileges: [
{
resource: { db: "", collection: "" },
actions: ["find"]
}
],
roles: []
})This creates a role named 'reportViewer' that can only read data from the 'reports' collection in the 'companyDB' database.
db.createRole({
role: "reportViewer",
privileges: [
{
resource: { db: "companyDB", collection: "reports" },
actions: ["find"]
}
],
roles: []
})You must have the 'userAdmin' or 'userAdminAnyDatabase' role to create custom roles.
Use specific actions like 'find', 'insert', 'update', 'remove' to control permissions.
Roles can include other roles by listing them in the 'roles' array for easier management.
Custom roles let you give users only the permissions they need.
You define roles by naming them and listing what actions they can do on which data.
Creating roles helps keep your database secure and organized.
Practice
Solution
Step 1: Understand role purpose
Custom roles are designed to control what actions a user can perform.Step 2: Match purpose to options
Only To assign specific permissions tailored to user needs describes assigning specific permissions tailored to user needs.Final Answer:
To assign specific permissions tailored to user needs -> Option BQuick Check:
Custom role purpose = Assign specific permissions [OK]
- Confusing roles with database backups
- Thinking roles speed up queries
- Assuming roles create collections
readWriteReports with read and write privileges on the reports database?Solution
Step 1: Check correct field names
The field for role name isname, notrole.Step 2: Verify privileges structure
Privileges must specifyresourcewithdbandcollection, andactionsas an array of strings.Final Answer:
db.createRole({name: "readWriteReports", privileges: [{resource: {db: "reports", collection: ""}, actions: ["find", "insert", "update"]}], roles: []}) -> Option CQuick Check:
Use 'name' for role name and specify privileges correctly [OK]
- Using 'role' instead of 'name' for role name
- Omitting collection field in resource
- Using 'readWrite' as action instead of specific actions
limitedUser?
db.createRole({
name: "limitedUser",
privileges: [
{ resource: { db: "sales", collection: "orders" }, actions: ["find"] }
],
roles: []
})Solution
Step 1: Analyze privileges actions
The action specified is only "find", which means read access.Step 2: Understand resource scope
The resource is the "orders" collection in the "sales" database, so permissions apply only there.Final Answer:
User can only read documents from sales.orders collection -> Option DQuick Check:
Action 'find' = read only on specified collection [OK]
- Assuming 'find' allows write operations
- Thinking permissions apply to whole database
- Confusing empty roles array as full access
db.createRole({
name: "dataEditor",
privileges: [
{ resource: { db: "analytics", collection: "reports" }, actions: "update" }
],
roles: []
})Solution
Step 1: Check 'actions' field type
The 'actions' field must be an array of strings, but here it is a single string "update".Step 2: Verify other fields
The 'resource' includes both db and collection correctly; 'name' is correct; 'roles' can be empty.Final Answer:
The 'actions' field should be an array, not a string -> Option AQuick Check:
'actions' must be an array [OK]
- Using string instead of array for 'actions'
- Confusing 'name' with 'role' field
- Thinking 'roles' array cannot be empty
reportManager that allows users to read all collections in the reports database and also run the aggregate action on the summary collection only. Which of the following commands correctly implements this?Solution
Step 1: Understand the requirements
Users must have read (find) access on all collections in 'reports' and aggregate only on 'summary' collection.Step 2: Check privileges for all collections
Using collection: "" means all collections in the database, so first privilege grants find on all collections.Step 3: Check aggregate privilege on summary
Second privilege grants aggregate only on 'summary' collection.Final Answer:
db.createRole({name: "reportManager", privileges: [{resource: {db: "reports", collection: ""}, actions: ["find"]}, {resource: {db: "reports", collection: "summary"}, actions: ["aggregate"]}], roles: []}) -> Option AQuick Check:
Separate privileges for all collections and specific collection actions [OK]
- Using db only without collection for find
- Combining find and aggregate on all collections
- Missing separate privilege for aggregate on summary
Ready to prove it?
Pass the test and this pattern moves unknown
