How to Configure MongoDB Slow Query Log

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up the mongodb slow query log by setting a time limit for database operations. The database then writes any query that takes longer than that limit to its log. Teams often give engineers broad database roles so they can check these slow queries. Broad roles let users run heavy commands or read private collections by mistake. How the mongodb slow query log and roles work MongoDB records slow operations to help teams find missing indexes and heavy workloads. When a query runs past your

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 slow query log by setting a time limit for database operations. The database then writes any query that takes longer than that limit to its log. Teams often give engineers broad database roles so they can check these slow queries. Broad roles let users run heavy commands or read private collections by mistake.

How the mongodb slow query log and roles work

MongoDB records slow operations to help teams find missing indexes and heavy workloads. When a query runs past your time threshold, the engine writes details to the log. Engineers also check these slow operations inside the database profiler collection. To set the threshold or read the log, a user needs the right database role.

MongoDB controls user actions through role-based authorization on specific databases. Each role gives a user permission to run a set of actions on defined resources. A role applies to its home database and can control permissions down to a collection. Self-hosted setups and cloud clusters have different built-in roles, but they share the same actions.

Teams often assign the read role so developers can check collection stats and indexes. This role allows actions like finding data, listing collections, listing indexes, and checking database stats. It does not let users insert, update, remove, or drop a collection. If an engineer needs to add an index to fix a slow query, they need more rights. The read and write role adds permissions to create or drop indexes and change data.

What native roles solve and what they leave open

Native database roles do a good job of deciding who can log in. They also decide which high-level actions a user can run on a database or collection. By splitting read roles from write roles, you stop developers from dropping collections by mistake. You can also limit who can change the logging threshold on a production cluster.

Still, native roles do not inspect what individual queries do while they run in flight. A user with a basic read role can still run an unindexed scan across millions of documents. That single query can lock up memory and slow down the whole database for everyone. Native roles also cannot filter out sensitive fields from the documents a query returns.

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

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

This gap matters when engineers inspect slow queries and sample data to fix performance bugs. Slow query logs and sample reads often include raw customer filter values and full documents. When an engineer runs a test query, the result returns emails, phone numbers, and card numbers. Without field-level masking in flight, every debugging session exposes private customer data to the user. Teams also lack a clear audit trail that ties each query to an individual person.

Best practices for safe query checks

Start by setting a realistic time threshold so the log only captures true bottlenecks. If the limit is too low, the log fills up with normal traffic and hides real issues. Give most engineers a read-only role so they can view stats without changing production state. Only grant index creation rights when a team member needs to apply a tested fix.

Pair your native roles with controls that check commands and rewrite sensitive fields in results. When masking rewrites emails, phone numbers, and national IDs in results, engineers can debug safely. They get the execution plan and timing data they need without exposing raw personal values. Always log who ran each diagnostic command so your security records stay complete.

Check MongoDB commands and documents on the wire

MongoDB roles and SCRAM login decide who opens a session. They do not check single commands or redact sensitive fields inside returned documents. With hoop.dev, you run an open-source proxy, a sidecar for your MongoDB, inline in the MongoDB wire protocol. On every command an AI agent or engineer runs against production, hoop.dev decides to pass, mask, block, or hold it for human review.

Before a command runs, hoop.dev guardrails check its effect and refuse banned drops or writes with MongoDB error code 13, or hold risky commands for approval. On the way back, hoop.dev data masking rewrites sensitive fields in returned documents in memory, and every verdict is logged. See the open-source code on GitHub.

Frequently asked questions

What does the slow query log capture in MongoDB?

It records operations that take longer to finish than your chosen time threshold. The log entry shows the query shape, execution time, scanned objects, and index use.

Can a user with the read role create an index to fix a slow query?

No, the basic read role only allows viewing data, collection lists, index lists, and stats. A user needs the read and write role or an admin role to create indexes.

Why do slow query checks create a data privacy risk?

Engineers often run sample queries on production collections to see why a command is slow. Without data masking, those queries return raw emails, card numbers, and phone numbers in results.

The query runs. The emails stay hidden.