How to Fix MongoDB Error 338 (ReshardCollectionInProgress)

Published
Length
3 min read

Luis Felipe Dias

All posts

You get mongodb error 338 when a command runs while a collection is already resharding. To fix it, wait for the active resharding task to finish or stop it safely. The database engine names this failure ReshardCollectionInProgress. It protects your data when two big structural tasks clash on the same cluster. Teams often hit this wall during busy maintenance windows or automated deployment runs. Root causes of mongodb error 338 in sharded clusters Sharded clusters split large data sets acros

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 get mongodb error 338 when a command runs while a collection is already resharding. To fix it, wait for the active resharding task to finish or stop it safely.

The database engine names this failure ReshardCollectionInProgress. It protects your data when two big structural tasks clash on the same cluster. Teams often hit this wall during busy maintenance windows or automated deployment runs.

Root causes of mongodb error 338 in sharded clusters

Sharded clusters split large data sets across multiple servers. Sometimes a team needs to change the shard key on a live collection. When you start this job, the cluster enters a strict state called ReshardCollectionInProgress.

During this time, the cluster copies chunks of data in the background. It also tracks new writes so no records get lost. If another script tries to start a second resharding job, the engine blocks it. It returns code 338 right away to stop data corruption.

Conflicting schema changes can also trigger this specific error code. Dropping an index or renaming a collection while resharding will fail. Automated deployment tools often cause this problem when they retry timed-out jobs. The first job is still running on the server when the second attempt arrives.

Long network delays can make client apps think a resharding request failed. The client disconnects, reconnects, and sends the exact same command again. The cluster sees the first task is still active and rejects the new request.

How to diagnose and fix the failure safely in production

Start by checking the active operations on your cluster before running new commands. Do not give every engineer full admin rights just to check this status. MongoDB grants access through role-based rules that apply down to a single collection.

Give your on-call responders the built-in read role for initial checks. The read role lets them run collection stats and database stats safely. It blocks them from running insert, update, remove, or drop collection actions 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.

Once you confirm an active resharding job is running, check its progress. Most of the time, the best fix is simply waiting for completion. Resharding moves huge amounts of data across servers and takes time to finish.

If the job is stuck and hurts app speed, an admin can stop it. Use an account with the proper admin role to abort the task cleanly. Never restart database nodes by force while a resharding task is in flight.

After the active job ends or aborts, check your deployment pipeline logs. Make sure your automation tools wait for long tasks instead of retrying right away. Adding a simple status check before schema steps prevents repeat failures in production.

Preventing repeat resharding conflicts in your workflow

Treat shard key changes as major production events that need clear team coordination. Only one person or pipeline should run structural commands at any given time. Lock your deployment queue when a cluster starts moving data chunks around.

Review your database user accounts after the maintenance window closes. Users with the readWrite role can create or drop indexes and collections. Keep those write permissions restricted to approved release pipelines and trusted operators.

When engineers inspect live collections during an incident, protect sensitive user records. Data masking rewrites matched fields like emails, phone numbers, card numbers, and national IDs in results. This lets your team check collection health while keeping private customer details safe.

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 ReshardCollectionInProgress error mean?

It means the database rejected your command because a collection is currently resharding. MongoDB returns code 338 to block conflicting tasks until the active job finishes.

Can I still read and write data during mongodb error 338?

Yes, normal app reads and writes keep working while a collection reshards. The error only blocks new resharding requests and conflicting admin commands.

How do I stop a stuck resharding operation safely?

An admin must send an abort command to the cluster coordinator. Do not kill server processes or drop collections while the task is still stopping.

The query runs. The emails stay hidden.