How to Fix MongoDB Error 361 (CollectionUUIDMismatch)

Published
Length
3 min read

Luis Felipe Dias

All posts

You see mongodb error 361 when a query sends an outdated collection ID. You fix this CollectionUUIDMismatch error by refreshing your metadata or sending the new ID. Every collection in the database has a unique internal ID alongside its human name. If a team drops and recreates a collection, the name stays the same. Still, the internal ID changes right away. Any app or worker still holding the old ID triggers this error. What causes mongodb error 361 in your cluster The database engine maps

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 361 when a query sends an outdated collection ID. You fix this CollectionUUIDMismatch error by refreshing your metadata or sending the new ID.

Every collection in the database has a unique internal ID alongside its human name. If a team drops and recreates a collection, the name stays the same. Still, the internal ID changes right away. Any app or worker still holding the old ID triggers this error.

What causes mongodb error 361 in your cluster

The database engine maps error code 361 to the name CollectionUUIDMismatch. This check protects your data from writing to the wrong place. When a client sends an expected collection ID, the server compares the two values. If the server sees a different ID, it blocks the request.

Most often, a schema job or an engineer drops and rebuilds a collection. While that change runs, background jobs or change streams keep the old ID in memory. When those jobs run their next read or write, the server rejects them.

Sharded clusters and replica sets can also show this issue during routing updates. If a router or client cache holds stale metadata, it sends the wrong ID. A fresh metadata check clears the old value and restores normal work.

How to diagnose and fix CollectionUUIDMismatch safely

Start your check with a safe read role on the target database. The built-in read role grants actions like listCollections, find, and collStats. It does not allow dropCollection, insert, update, or remove actions. Using a read role lets you inspect collection IDs without risking another drop.

Compare the current collection ID on the server with the ID in your app logs. If they differ, your app or worker holds a stale reference in memory. Restart the worker or refresh the driver pool so it fetches current metadata.

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

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Next, check why the collection ID changed in the first place. Look for jobs or users with the readWrite role on that database. The readWrite role includes createCollection and dropCollection actions along with data changes. Since roles grant privileges down to the collection level, limit who can drop collections.

Preventing stale collection IDs during production changes

Do not disable UUID checks to bypass this error in production. Those checks stop scripts from modifying a rebuilt collection under false assumptions. Instead, coordinate schema drops with your running apps and change stream readers.

Pause background workers before you drop and recreate any target collection. Once the new collection exists, let your apps refresh their metadata cache. If you inspect live records during a fix, make sure masking rewrites emails and phone numbers.

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 CollectionUUIDMismatch mean in MongoDB?

It means the collection ID in your request does not match the current collection ID. This happens when someone drops and recreates a collection under the same name.

Can I check collection metadata without write access?

Yes, the built-in read role includes the listCollections and collStats actions on non-system collections. This lets engineers check current collection details without having dropCollection or update privileges.

How do I stop apps from hitting error 361 again?

Restrict dropCollection rights by scoping roles down to the collection level. Also, refresh client pools and restart change streams after any planned collection rebuild.

Teams that document these role rules and review logs each month catch config drift early. Clear ownership for each role keeps live systems simple to run and easy to audit across the whole team.

The query runs. The emails stay hidden.