How to Configure Haystack MongoDB Prevent Delete

Published
Length
3 min read

Luis Felipe Dias

All posts

To set up a haystack mongodb prevent delete rule, restrict the database user role. You must remove destructive privilege actions like remove and dropCollection from the account. Teams often build AI pipelines using a standard readWrite database role for speed. That broad role lets the app insert, update, remove, and drop whole collections. If a pipeline bug or bad prompt triggers a delete call, data disappears fast. How to set up haystack mongodb prevent delete MongoDB controls what an app ca

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.

To set up a haystack mongodb prevent delete rule, restrict the database user role. You must remove destructive privilege actions like remove and dropCollection from the account. Teams often build AI pipelines using a standard readWrite database role for speed. That broad role lets the app insert, update, remove, and drop whole collections. If a pipeline bug or bad prompt triggers a delete call, data disappears fast.

How to set up haystack mongodb prevent delete

MongoDB controls what an app can do through role-based rules. A role grants specific privilege actions on defined database resources. You can scope these rules down to the collection level of detail. Self-hosted servers and MongoDB Atlas use the same core privilege actions for roles.

First, look at how your Haystack pipeline uses the document store. Many retrieval pipelines only need to search and read stored records. For those apps, assign the built-in read role to the database user. The read role allows actions like find, listCollections, and listIndexes on non-system collections. It also allows dbStats, collStats, and changeStream for basic work. It leaves out insert, update, remove, and dropCollection actions.

Some Haystack workflows also need to write new embeddings or update records. The built-in readWrite role allows insert and update, but it also allows remove. It even allows dropIndex and dropCollection on that database. To let Haystack write data while blocking deletes, build a custom database role. Grant the read actions plus insert and update on the target collection. Leave out the remove and dropCollection actions from this custom role. Assign this custom role to the credentials that your Haystack app uses.

What native roles solve and what they leave open

Database roles do a good job of stopping unauthorized actions at login time. When Haystack logs in with a restricted role, the database blocks remove calls. This protects your collections from accidental drops or bulk delete runs. It keeps the core document store safe from simple code mistakes.

Still, static database roles only check who logs into the database. They do not inspect what individual queries or updates do in flight. A pipeline with update access can still overwrite records with blank values. That kind of bad update destroys data just like a delete command. Static roles also apply the same rules to every query from that app. You cannot ask a human to approve a rare cleanup task on the fly.

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

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Native roles also do not rewrite sensitive fields inside query results. When Haystack pulls documents for a prompt, the database returns full records. Those records might hold emails, phone numbers, card numbers, or national IDs. Database roles alone cannot mask those fields in query results before Haystack reads them. Teams also lack a clear log tying each database call to a specific user.

How to test and maintain your database roles

After you assign the new role, test your Haystack pipeline in staging. Run a normal search workflow to confirm that find queries still work well. Next, run an indexing pipeline to check that insert and update calls succeed. Then trigger a delete call from the document store to test your block. The database should reject the remove action and return a permission error.

Review your database users every few months to keep permissions tight. Separate your indexing credentials from your search credentials whenever you can. Give the search pipeline the plain read role so it cannot change data. Give the indexing pipeline the custom role that allows writes without deletes. This split keeps day-to-day search traffic from changing or wiping your store.

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. It reads the MongoDB wire protocol and checks commands and bulk writes as JSON while passing login through untouched.

Before a command runs, hoop.dev guardrails check its effect and refuse banned drops or writes with MongoDB error code 13. On the way back, hoop.dev data masking rewrites sensitive fields in returned documents that match your rules. Every command and verdict is logged. See the open-source code on GitHub.

Frequently asked questions

Can the built-in readWrite role block delete commands in MongoDB?

No, the built-in readWrite role always includes the remove and dropCollection privilege actions. To allow writes while blocking deletes, you must define a custom role without remove.

Does the MongoDB read role allow Haystack to search collections?

Yes, the read role grants the find action on all non-system collections. This allows Haystack retrievers to query documents without having permission to insert or remove data.

Can I restrict delete permissions on just one MongoDB collection?

Yes, MongoDB roles can grant access down to the collection level. You can allow writes without deletes on your main collection while keeping other collections read-only.

The query runs. The emails stay hidden.