How to Configure MongoDB Service Account

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up a mongodb service account by creating a dedicated database user. Then you assign specific roles that limit actions on target databases or collections. Many engineering teams start by sharing one broad database user across several backend apps. When permissions stay too wide, a single compromised key or bad script can delete collections. Tightening these roles early stops apps from touching data they do not need. How a mongodb service account works with roles MongoDB controls access

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 set up a mongodb service account by creating a dedicated database user. Then you assign specific roles that limit actions on target databases or collections. Many engineering teams start by sharing one broad database user across several backend apps. When permissions stay too wide, a single compromised key or bad script can delete collections. Tightening these roles early stops apps from touching data they do not need.

How a mongodb service account works with roles

MongoDB controls access through role-based rules. Each role pairs a set of permitted actions with a defined database or collection. A role applies to the database where you define it. It can grant access down to a single collection level. Self-hosted deployments and MongoDB Atlas use different built-in roles. Still, both build those roles from the same core privilege actions.

For apps that only fetch records, the built-in read role fits well. This role allows read access to all non-system collections in its database. It also covers the system JavaScript collection. It grants actions to find data, list collections, list indexes, and watch change streams. It also lets the account check database stats and collection stats. It blocks actions that insert, update, or remove records, or drop a collection.

Apps that change records need the read and write role instead. This role includes every action in the read role plus data modification actions. It lets the account insert, update, and remove documents. It also allows the user to create or drop collections and indexes.

Scoping custom privileges for machine identities

Built-in roles cover entire databases, which is often too wide for one service. A billing worker might only need to update one orders collection. If you give that worker a database-wide write role, it can change user tables too. You can avoid this risk by creating a custom role for the service account.

Custom roles let you pick exact actions on specific collections inside a database. You can grant find and update actions on one collection and nothing else. You should also give each app or worker its own separate account. Separate accounts make it easy to rotate keys or revoke access for one service. If a key leaks, you only update one service instead of every app.

Service-to-Service Authentication + Service Account Governance: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

What native roles solve and what they leave open

Native roles do a great job deciding which account can log in. They also limit which collections an account can read or change. Still, database roles stop checking once the session starts and the collection matches. They do not inspect what individual commands or queries do in flight.

A service account with read access can still pull full documents containing sensitive fields. Any query on a permitted collection returns raw emails, phone numbers, card numbers, and national IDs. Likewise, an engineer or script using a write account can run a broad delete command. Because the role allows collection writes, the database runs the destructive query right away. Native roles also cannot require a peer review before a risky command runs.

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 do you make a mongodb service account read-only?

Assign the built-in read role on the target database when you create the user. This grants find and stats actions while blocking insert, update, remove, and drop actions.

Can a mongodb service account access only one collection?

Yes, MongoDB roles can grant access down to the collection level. You create a custom role that lists allowed actions for that specific collection only.

Why do database roles still allow sensitive data exposure?

Roles check collection permissions at the gate, not the fields inside returned documents. If a role allows reads on a collection, queries return raw card numbers and emails unless masking rewrites matched fields in results.

The query runs. The emails stay hidden.