How to Configure MongoDB Create User

Published
Length
3 min read

Luis Felipe Dias

All posts

You run a mongodb create user workflow by defining an account and assigning roles. Each role grants specific actions on databases or collections to control what users do. Teams often start by creating a few shared accounts for developers and apps. Over time, these shared accounts accumulate broad permissions across multiple databases. When permissions grow too wide, one mistake can drop collections or expose sensitive data. How the mongodb create user workflow works MongoDB manages database p

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

You run a mongodb create user workflow by defining an account and assigning roles. Each role grants specific actions on databases or collections to control what users do. Teams often start by creating a few shared accounts for developers and apps. Over time, these shared accounts accumulate broad permissions across multiple databases. When permissions grow too wide, one mistake can drop collections or expose sensitive data.

How the mongodb create user workflow works

MongoDB manages database permissions through role-based rules. When you create an account, you link it to a specific login database. That database holds the user credentials and role assignments for that account. A role pairs a set of allowed actions with a target resource. It applies to the database where you define it. Roles can restrict privileges down to the collection level of detail.

Note that self-hosted deployments and MongoDB Atlas offer different built-in roles. Still, both setups build their roles from the same core privilege actions. Admins can pick built-in roles or build custom roles for specific collections. Every user login checks these assigned roles before the database runs an action.

Choosing between read and write roles

Most teams start with two basic database roles when setting up accounts. The read role lets users view all non-system collections in a database. It also grants read permissions on the system JavaScript collection. This role permits actions like finding documents, listing collections, and checking index stats. It also allows viewing database stats, checking collection stats, and opening change streams. It blocks users from inserting, updating, removing, or dropping collections.

The read and write role includes every action from the basic read role. It adds the power to change data inside that database. Users with this role can insert, update, and remove documents. They can also create or drop collections and manage indexes. Giving this role to a human user carries real operational risk. A single bad command can wipe out production records or remove critical indexes.

What native user roles solve and what they miss

Native database roles do a good job of sorting users into broad groups. They stop a reporting tool from writing data or dropping a collection. They also keep an app locked to its own database. Still, native roles only check permissions at the database or collection level.

User Provisioning (SCIM) + MongoDB Authentication & Authorization: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Once a user logs in with a read role, they can read everything. The database cannot hide specific document fields from that user during a query. If a collection holds emails, phone numbers, card numbers, or national IDs, queries return them. Standard roles cannot rewrite matched fields in results to protect private data.

Also, static user accounts stay active until an admin removes them. Shared credentials often end up in local text files or chat logs. Native user creation decides who logs in, not what individual queries do in flight. Teams still need a way to inspect commands and mask fields before results return.

Inspect MongoDB wire traffic in flight

Built-in MongoDB roles govern collection access at setup time. They cannot score command risk per call or mask sensitive document values on the fly. By design, hoop.dev sits inline in the MongoDB protocol next to your server as a sidecar for your MongoDB. On every production command from mongosh, drivers, or AI agents, hoop.dev decides whether to pass, mask, block, or hold it for human review.

First, hoop.dev guardrails block bad deletes, drops, and write pipelines or hold them for approval. Next, hoop.dev data masking rewrites sensitive values in returned documents in memory and logs each decision. Explore the repo on GitHub.

Frequently asked questions

How fine-grained are native MongoDB roles?

MongoDB roles work down to the collection level within a defined database. They cannot restrict read permissions to specific fields or individual documents on their own.

What is the difference between read and readWrite roles?

The read role allows finding data, viewing stats, and listing collections or indexes. The readWrite role adds actions to insert, update, remove, and drop collections or indexes.

Do MongoDB Atlas and self-hosted setups use the same roles?

MongoDB Atlas database users have different built-in roles than self-hosted deployment users. Still, both setups use the exact same underlying privilege actions to control permissions.

The query runs. The emails stay hidden.