How to Fix MongoDB Error 166 (CommandNotSupportedOnView)
You get mongodb error 166 when you run a banned command on a view. To fix it, run write or index commands on the base collection instead. In the database engine, code 166 means CommandNotSupportedOnView. Views in this database are read-only objects built from queries. They do not store their own documents on disk. Because of this design, the engine blocks many actions on views. When an app or user tries those actions, the server stops the request. Why mongodb error 166 happens in your databas
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 166 when you run a banned command on a view. To fix it, run write or index commands on the base collection instead.
In the database engine, code 166 means CommandNotSupportedOnView. Views in this database are read-only objects built from queries. They do not store their own documents on disk. Because of this design, the engine blocks many actions on views. When an app or user tries those actions, the server stops the request.
Why mongodb error 166 happens in your database
A view acts like a saved query on top of a real collection. When you read from a view, the engine runs that query right then. You can use the find action to read data from a view. Still, you cannot use insert, update, or remove actions on it.
Many teams see this error when they treat a view like a collection. A developer might try to build an index on a view. Since views have no stored data, index actions like createIndex and dropIndex fail. The database throws code 166 to protect the system state.
Data changes also trigger CommandNotSupportedOnView in production apps. If an app sends a write request to a view name, it fails. Collection management tasks can also cause this exact error. For example, trying to rename a view with collection tools will fail.
Sometimes, backup tools or migration scripts trigger this error code. If a script tries to write documents into every listed object, it hits views. The script does not check if the object is a view first. When it tries an insert action on a view, the engine blocks it.
How to find and fix the error in production
Start by checking your database logs to find the failing command. Look for the exact object name that caused code 166. Next, check your database catalog to see which objects are views. The listCollections action shows all collections and views in your database.
Once you find the view, check the definition behind it. Every view points to a source collection in the same database. If you need to add an index, add it to the source collection. The view will use the index on the source collection during reads.
Customer Support Access to Production + MongoDB Authentication & Authorization: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
If an app is trying to write data, update the app settings. Point your insert, update, and remove calls to the base collection. Keep using the view name only for your read queries. This splits your read paths and write paths cleanly.
You should also check your database roles to prevent future mistakes. The database grants access through roles down to the collection level. The built-in read role grants actions like find, listCollections, and listIndexes. It does not allow insert, update, remove, or dropCollection actions.
The readWrite role adds actions like insert, update, remove, and createIndex. Assign the read role to users and apps that only query views. Do not give full readWrite privileges to services that only read views. Good role rules help teams avoid running write commands on the wrong objects.
How to prevent view command errors across teams
Clear naming rules help engineers tell views and collections apart. Many teams add a short prefix or suffix to every view name. When an engineer sees that name, they know it is read-only. They will not try to run createIndex or update on it.
You should also test migration scripts in a staging setup first. Make sure your tools filter out views before running write tasks. Check the object type from the listCollections output before running changes. This simple check stops code 166 before it reaches production.
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
What does CommandNotSupportedOnView mean in plain terms?
It means you ran a command on a view that only works on collections. This happens most often with write commands like insert or index commands like createIndex.
Can I add an index to a view to fix slow reads?
No, you cannot run createIndex on a view without getting code 166. Instead, run createIndex on the source collection that feeds the view.
Does the readWrite role let me insert data into a view?
No, roles enforces policy on the engine rules for views. Even with the readWrite role, you must send insert, update, and remove commands to the base collection.
Related guides
In this post