How to Fix MongoDB Error 402 (ResourceExhausted)
You see mongodb error 402 when the database runs out of system resources. Fix it by retrying failed queries, tuning heavy workloads, or adding cluster capacity. MongoDB names this failure code ResourceExhausted and places it in the RetriableError group. Your app can wait a short moment and run the same request again. Still, repeated failures mean your server needs workload changes or larger hardware limits. What causes mongodb error 402 in your cluster Database servers rely on memory, proces
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 see mongodb error 402 when the database runs out of system resources. Fix it by retrying failed queries, tuning heavy workloads, or adding cluster capacity.
MongoDB names this failure code ResourceExhausted and places it in the RetriableError group. Your app can wait a short moment and run the same request again. Still, repeated failures mean your server needs workload changes or larger hardware limits.
What causes mongodb error 402 in your cluster
Database servers rely on memory, processor time, disk space, and active network pools. When incoming queries consume all available capacity, the engine rejects new work. It returns code 402 to protect the node from a complete crash.
Sudden traffic spikes from client apps often trigger this resource exhaustion state. Unindexed queries also force the engine to scan whole collections in memory. Large batch writes can lock up disk queues and exhaust internal thread pools.
Ad-hoc queries from engineers or automated scripts add extra load during peak hours. One bad query across a huge collection can starve normal app traffic. Because the error is a RetriableError, drivers might retry right away and add load.
How to diagnose and fix resource exhaustion safely
Start by checking your server metrics for CPU, memory, and disk spikes. Look for slow queries or sudden jumps in open client connections. You do not need full admin rights to check database health safely.
MongoDB controls user permissions through roles on a specific database or collection. The built-in read role grants read access to all non-system collections. It allows actions like find, listCollections, listIndexes, dbStats, collStats, and changeStream.
MongoDB Authentication & Authorization + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Assigning the read role lets responders inspect collection stats without changing any data. The read role blocks insert, update, remove, and dropCollection actions by design. Avoid giving engineers the readWrite role during an active incident response.
The readWrite role allows data changes and index creation or removal. Dropping or building an index during high load can make resource exhaustion worse. Keep diagnostic sessions restricted so a mistake cannot hurt an overloaded cluster.
Preventing future resource exhaustion incidents
Once you find the heavy query or traffic spike, cut the load. Add missing indexes during low traffic windows so queries scan fewer records. Set client drivers to use backoff delays when they retry error 402.
Without a pause between retries, clients will hammer an already busy server. You should also review which users and scripts can run heavy commands. Limit ad-hoc queries in production and mask sensitive fields like emails or phone numbers.
Note that MongoDB Atlas users have different built-in roles than self-hosted users. Both deployment types still build their roles from the same privilege actions. Scope each role down to the exact collection needed for daily work.
Add command guardrails and field masking beside MongoDB
Collection roles and IP rules control who connects to MongoDB. Yet an active client or agent session can still run a pipeline that writes data or reads raw personal fields. In practice, hoop.dev sits inline in the wire protocol beside your database as a sidecar for your MongoDB. For every command an agent or human sends in production, hoop.dev decides to pass, mask, block, or escalate it for review.
Using hoop.dev MongoDB controls, guardrails refuse banned commands before they run or hold risky calls for approval, and data masking rewrites matched fields in returned documents in memory. Furthermore, hoop.dev holds SOC 2 Type II, is MIT licensed, and generates evidence for GDPR, HIPAA, and PCI DSS programs. View the project on GitHub.
Frequently asked questions
Is mongodb error 402 safe to retry in my app?
Yes, MongoDB marks code 402 (ResourceExhausted) as a RetriableError in its official spec. Always use a short backoff delay so retries do not overload the server.
Which MongoDB role should engineers use to check collection stats?
Use the built-in read role on the target database for safe checks. It includes dbStats, collStats, and listIndexes while blocking all data write actions.
Can too many write operations trigger ResourceExhausted errors?
Yes, heavy insert or update batches can use up memory and disk queues. Give write permissions only to apps that need the readWrite role.
Related guides
In this post