Error
Error Code:
166
MongoDB Error 166: Command Not Supported On View
Description
This error signifies that a database command or operation was attempted on a MongoDB view, but the specific command is not supported for views. Views are read-only and do not permit operations that modify data, create indexes, or alter the database structure.
Error Message
Command Not Supported On View
Known Causes
4 known causesAttempting Write Operations
This error occurs when a command designed to insert, update, or delete data is executed against a view, which is inherently read-only.
Using Unsupported Aggregation Stages
Certain aggregation pipeline stages, such as '$out' or '$merge', are not permitted on views as they attempt to write data or create new collections.
Index Management Operations
Views do not have their own indexes. Attempting to create, drop, or manage indexes directly on a view will result in this error.
Schema Modification Commands
Commands that alter the schema or structure of a collection, such as 'collMod' or renaming, cannot be applied to a view.
Solutions
3 solutions available1. Execute Command on Underlying Collection easy
Modify your operation to target the collection the view is based on, not the view itself.
1
Identify the underlying collection(s) used to define the view. You can find this information in the view's definition.
db.getCollectionInfos({ name: 'yourViewName' })
2
Rewrite your command (e.g., `aggregate`, `find`, `update`, `delete`, `count`) to operate directly on the identified underlying collection(s) instead of the view.
// Instead of:
db.yourViewName.aggregate([...])
// Use:
db.yourUnderlyingCollection.aggregate([...])
2. Adapt Aggregation Pipeline for View Limitations medium
Adjust your aggregation pipeline to exclude stages not supported on views.
1
Review the MongoDB documentation for aggregation pipeline stages that are supported on views. Common unsupported stages include `$merge`, `$out`, and certain administrative commands.
2
If your pipeline uses unsupported stages, consider alternative approaches. For example, instead of `$merge` or `$out` to write to another collection, you might need to perform that operation separately on the underlying collection after fetching data from the view.
// Example: If you were trying to use $merge on a view to create a new collection
// This is not supported on views. Instead, fetch data and then insert/update into a collection.
const dataFromView = db.yourViewName.aggregate([...]);
db.yourNewCollection.insertMany(dataFromView);
3
If the unsupported stage is crucial for your logic, you may need to rethink the view's purpose or your overall data processing strategy.
3. Use a Temporary Collection for Complex Operations medium
Materialize view data into a temporary collection to perform unsupported operations.
1
Create a temporary collection to hold the results of your view query.
db.createCollection('tempViewResults')
2
Populate the temporary collection with data from the view. This will likely involve an `aggregate` command on the view.
const viewData = db.yourViewName.aggregate([...]);
db.tempViewResults.insertMany(viewData);
3
Perform your unsupported command (e.g., `$merge`, `$out`) on the temporary collection.
db.tempViewResults.aggregate([
{ $match: { /* your conditions */ } },
{ $merge: { into: 'anotherCollection', on: '_id', whenMatched: 'replace', whenNotMatched: 'insert' } }
])
4
Optionally, drop the temporary collection once the operation is complete.
db.tempViewResults.drop()