How to Fix MongoDB Error 346 (ChangeStreamInvalidated)

Published
Length
3 min read

Luis Felipe Dias

All posts

You see mongodb error 346 when a change stream closes after a drop or rename event. To fix it, catch the error in your app and open a new change stream. In the official database rules, error code 346 carries the name ChangeStreamInvalidated. The database sends this signal when the watched target changes in a big way. Your app cannot keep reading from the old stream after this event happens. You must understand why the stream stopped and how to restart it safely. What causes mongodb error 346

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 346 when a change stream closes after a drop or rename event. To fix it, catch the error in your app and open a new change stream.

In the official database rules, error code 346 carries the name ChangeStreamInvalidated. The database sends this signal when the watched target changes in a big way. Your app cannot keep reading from the old stream after this event happens. You must understand why the stream stopped and how to restart it safely.

What causes mongodb error 346 in your database

A change stream watches a collection, a database, or a whole cluster for data edits. When someone drops or renames a watched collection, the stream loses its target. Dropping the whole database also breaks any stream watching that database. The engine stops the cursor and returns code 346 to the client right away.

This failure often happens during schema updates or routine cleanup jobs in production. An admin or an automated job might drop a temp table while workers watch it. The worker app tries to pull the next batch of events from the closed cursor. The server rejects the read request and throws the ChangeStreamInvalidated code.

Role permissions play a huge part in how these drop events happen. MongoDB grants access through role-based rules that allow actions on specific resources. A role applies to its home database and can control access down to one collection. Built-in roles in Atlas differ from self-hosted ones, yet both use the same action sets.

The built-in read role grants read access to all non-system collections in a database. It allows actions like find, listCollections, listIndexes, dbStats, collStats, and changeStream. It does not allow the insert, update, remove, or dropCollection actions. In contrast, the readWrite role adds the power to change data and drop collections.

How to diagnose and fix the failure safely

First, check your database logs to see what action broke the change stream. Look for drop or rename commands that ran right before the error showed up. Often, a developer or script has the readWrite role when it only needs read. Because readWrite includes the dropCollection action, a bad script can wipe out watched tables.

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

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

To prevent surprise drops, tighten your database roles right away. Give your stream workers and reporting apps the standard read role on that database. Since the read role includes the changeStream action, your app can still watch for changes. Still, those users cannot run dropCollection or remove data by mistake.

Next, update your app logic to handle the ChangeStreamInvalidated signal cleanly. Do not try to resume the exact same stream from the invalidate event token. If the collection is gone, resuming at that exact point will just fail again. Instead, close the dead cursor, wait for the collection to exist, and start a fresh stream.

Preventing accidental drops in production

Many teams hit this error when engineers run manual fixes in live databases. If every engineer logs in with a shared role that can drop tables, mistakes happen. You should keep day-to-day database access locked to read-only rules by default. Only allow write or drop actions when a team member gets approval for a task.

You should also protect private user data when engineers inspect collections to debug stream errors. Data masking rewrites matched fields in results, such as emails, phone numbers, and card numbers. This lets developers check broken records while keeping sensitive fields hidden in query outputs. Good access controls and clean error handling will keep your change streams running well.

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

ChangeStreamInvalidated is the official name for mongodb error 346. It means the watched collection or database was dropped or renamed, so the stream closed.

Which built-in role lets an app open a change stream safely?

The built-in read role includes the changeStream action on non-system collections in its database. It lets your app watch data without granting the dropCollection action.

Can I resume a change stream right after error 346?

You cannot continue the old cursor after it becomes invalid. Your app must recreate the collection if needed and open a brand new change stream.

The query runs. The emails stay hidden.