MongoDB Tutorial 0/120 lessons ~6 min read Lesson 78

    Authorization

    Authorization decides what an authenticated identity is allowed to do.

    Course progress0%
    Focus
    12 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Authorization decides what an authenticated identity is allowed to do. In MongoDB this is implemented with role-based access control (RBAC): every user is assigned one or more roles, and every role grants a set of privileges (actions × resources).

    Authentication answers "who are you?". Authorization answers "what can you touch?". The two are independent — a user with valid credentials but no roles can connect and do absolutely nothing, which is exactly what you want.

    Learning objective: Apply Authorization correctly in a TechLearningPRO MongoDB workload — with clear data-model choices, safe writes, and failure modes you can explain in review.

    Treat every example as production-shaped MongoDB: explicit projections, indexes that match the access path, and Atlas-safe defaults. Prefer mongosh / Node driver shapes used in SaaS backends.

    Purpose of this lesson

    Apply Authorization correctly in a TechLearningPRO MongoDB workload — with clear data-model choices, safe writes, and failure modes you can explain in review.

    Understanding the topic

    MongoDB's RBAC model:

    • Privilege = { resource, actions }. E.g. { db: "shop", collection: "orders" } + ["find", "insert"].
    • Role = a named bundle of privileges. Built-in: read, readWrite, dbAdmin, clusterAdmin, backup, restore.
    • User = identity + assigned roles, scoped to one or more databases.
    • Custom roles let you grant exactly what an app needs (read all orders, write only audit logs).
    • Atlas database roles map cluster roles to Atlas project-level access for cloud-native RBAC.

    Step-by-step explanation

    1. State when Authorization is the right MongoDB tool versus an alternative.
    2. Map the diagram boxes to driver, mongod, WiredTiger, and your collection.
    3. Run the smallest correct mongosh / driver example against a TechLearningPRO learners collection.
    4. Name two failure modes (missing index, bad shard key, unbounded scan) and how you detect them.

    Informative example

    Create a least-privilege application role and assign it to a service account:

    js
    // 1. Custom role: app can read products, write orders, never touch users.
    db.adminCommand({
    createRole: "shopApp",
    privileges: [
    { resource: { db: "shop", collection: "products" }, actions: ["find"] },
    { resource: { db: "shop", collection: "orders" }, actions: ["find", "insert", "update"] }
    ],
    roles: []
    });
    // 2. Service account using the role
    db.getSiblingDB("shop").createUser({
    user: "checkout-svc",
    pwd: passwordPrompt(),
    roles: [{ role: "shopApp", db: "admin" }]
    });
    // 3. Verify
    db.getSiblingDB("shop").runCommand({ rolesInfo: 1, showPrivileges: true });

    Real-world use

    Healthcare and fintech teams use custom roles to enforce HIPAA / SOX separation of duties: analysts get read on de-identified collections only, app services get scoped readWrite, and DBAs get cluster admin via short-lived federated credentials — never shared passwords.

    Security implications

    MongoDB is a trust boundary. Drivers must not interpolate untrusted input into query operators.

    • Never build queries by concatenating user strings into operator objects.
    • Use least-privilege Atlas database users; apps should not have root.
    • Enable TLS and avoid exposing mongod on public networks.

    Best practices

    • Least privilege always. Never assign readWriteAnyDatabase to an app.
    • One role per app, per environment. The staging app should not be able to write to prod.
    • Rotate service-account passwords via secrets manager (Vault, AWS SM, Atlas API).
    • Audit role assignments quarterly with db.getUsers() and Atlas access logs.

    Common mistakes

    • Granting dbOwner "just to get it working" — it never gets removed.
    • Sharing one user between many microservices — you lose attribution in audit logs.
    • Forgetting that __system role exists; never assign it to humans.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1BeginnerQuestionWhat is Authorization and when do you use it?+

    Answer

    Authorization in MongoDB = RBAC: roles bundle privileges, users get roles.
    2IntermediateQuestionHow would you verify Authorization is working in production?+

    Answer

    Always design custom least-privilege roles per app and environment.
    3AdvancedQuestionWhat breaks if you misuse Authorization?+

    Answer

    Atlas federated auth + secrets rotation removes shared-password risk.

    Hands-on exercise

    Hands-on drill:

    • Implement the smallest correct example for Authorization on a learners/orders-like collection.
    • Capture explain('executionStats') and confirm an index was used when expected.

    Summary

    Authorization is a production MongoDB skill: model for your read path, prove it with explain(), and ship with indexes and least-privilege roles.

    Ready to mark this lesson complete?Track your journey across the entire course.