RBAC
RBAC: free step-by-step lesson with examples, common mistakes, and interview tips — part of MongoDB Tutorial on Toolliyo Academy.
On this page
MongoDB Tutorial · Lesson 74 of 100
RBAC
Foundations & CRUD ✓ → Queries & Schema ✓ → Aggregation & Scale ✓ → Atlas & Projects
Atlas & Projects · 4 — Build · ~10 min · MongoDB — Atlas & Security
What is this?
RBAC (role-based access control) groups privileges into roles, then assigns roles to users. MongoDB ships built-in roles and lets you define custom roles with fine-grained actions.
Why should you care?
Enterprises map “PaymentsService”, “SupportRO”, and “DBA” to roles once, then onboard humans/services without inventing privileges each time.
See it live — copy this example
Open mongosh or MongoDB Compass, select database nosqlverse, then run the example. Change one field and run again.
use admin
db.createRole({
role: "orderReader",
privileges: [{
resource: { db: "NoSQLVerse", collection: "orders" },
actions: ["find"]
}],
roles: []
})
db.createUser({
user: "supportBot",
pwd: "strong-pass",
roles: [ { role: "orderReader", db: "admin" } ]
})
Run Example »
Edit the code below and click Run to see the result in Toolliyo’s live editor.
What happened?
- orderReader may only find on orders.
- supportBot inherits that role.
- It cannot read users or write orders.
- Custom roles encode company policy in the database.
Practice next
- Create a custom role limited to one collection.
- Assign it to a test user.
- Attempt forbidden actions and confirm failure.
- Add update action for a support “refund note” field only via careful app design.
- List roles with db.getRoles({ showPrivileges: true }).
Remember
RBAC assigns roles, not raw privileges, to users. Custom roles scope collections and actions. Align roles with services and job functions.
Bank-grade separation of duties
Writers, readers, and admins map to different Mongo roles audited quarterly.
Outcome: Compliance can prove who could touch which collections.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!