How to Configure MongoDB Atlas Slow Query Log

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up the mongodb atlas slow query log by turning on database profiling and setting a time threshold for slow reads and writes. When a query takes longer than your limit, the database records the operation details so your team can review them. Teams often share broad database roles so developers can inspect these logs and fix slow requests. Still, broad permissions expose live collections and let users run unapproved commands across the cluster. How the mongodb atlas slow query log works

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 the mongodb atlas slow query log by turning on database profiling and setting a time threshold for slow reads and writes. When a query takes longer than your limit, the database records the operation details so your team can review them. Teams often share broad database roles so developers can inspect these logs and fix slow requests. Still, broad permissions expose live collections and let users run unapproved commands across the cluster.

How the mongodb atlas slow query log works

A database cluster tracks how long each read and write operation takes to finish. When you turn on slow query logging, you define a time limit for normal requests. Any query that runs past that limit gets written to a diagnostic log stream. The log captures the query shape, the collection name, and the execution time.

Engineers read these records to find missing indexes and full collection scans. To check the logs or test a slow query, a user must log in with a database role. MongoDB uses role-based authorization to grant sets of actions on specific resources. A role applies to the database where you define it and can grant permissions down to a single collection.

Atlas database users have different built-in roles than self-hosted users, though both use the same privilege actions. Many teams give developers a read role so they can run find commands and check collection stats. Other teams grant a read-write role so engineers can create or drop indexes while tuning slow queries.

What native roles solve and what they leave open

Native database roles decide who can log in and which collections they can touch. The built-in read role lets a user read all non-system collections in a database. It allows actions like finding documents, listing collections, listing indexes, and checking database stats. It blocks actions that change data, such as inserting, updating, removing, or dropping collections.

If an engineer needs to add an index to fix a slow query, they need stronger permissions. The read-write role includes all read actions plus the power to change data and manage indexes. While this setup controls access at the collection level, it leaves an operational gap during live sessions.

A role checks permissions at login, not what individual statements do in flight. First, a developer with read access to a collection can view raw fields inside returned documents. If a table holds emails, phone numbers, card numbers, or national IDs, a test query returns them in plain text. Second, the slow query log itself can capture literal filter values from slow requests. Third, giving someone read-write access just to build an index also lets them delete records or drop a collection by mistake.

MongoDB Authentication & Authorization + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Managing query tuning and data visibility in flight

Safe query tuning requires separating diagnostic work from unrestricted data access. Developers need to see query plans and index usage without pulling sensitive user records onto their laptops. When teams inspect slow queries, they should test performance with masked results rather than raw production rows. Data masking rewrites matched fields like emails and card numbers in query results before they reach the client.

Teams also need fine control over index changes during tuning sessions. Instead of handing out permanent read-write credentials, teams can review and approve single index creation commands. Every query run during a tuning session should tie back to an individual user identity, not a shared role. This approach keeps query tuning fast while protecting live collections from accidental writes and data exposure.

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

What causes an operation to appear in the slow query log?

An operation appears in the log when its run time exceeds your configured time limit. Common causes include missing indexes, large collection scans, and heavy sort operations on unindexed fields.

Can the built-in read role create indexes to fix slow queries?

No, the built-in read role only allows read actions like finding documents and listing existing indexes. Creating or dropping an index requires privileges found in the read-write role or a custom role.

Do slow query logs and diagnostic queries expose sensitive fields?

Yes, slow query logs can record query filter values, and test queries return full documents to the user. Adding field masking rewrites emails, phone numbers, card numbers, and national IDs in results during fixing.

Short role checks help teams work fast. Simple rules make each review clear. Good logs keep every team safe. Scoped keys stop bad changes early.

The query runs. The emails stay hidden.