Error
Error Code: 227

MongoDB Error 227: Implicit Collection Creation Disabled

📦 MongoDB
📋

Description

This error occurs when MongoDB attempts to automatically create a new collection during an insert or update operation, but the current database configuration or user permissions prevent this. It indicates that an operation targeted a collection that does not exist, and the system is not permitted to create it on the fly.
💬

Error Message

Cannot Implicitly Create Collection
🔍

Known Causes

3 known causes
⚠️
Auto Collection Creation Disabled
The `autoCreateCollections` parameter in the database or deployment configuration has been explicitly set to `false`, preventing automatic collection generation.
⚠️
Insufficient User Privileges
The user account performing the write operation lacks the necessary `createCollection` privilege to automatically create new collections in the specified database.
⚠️
Collection Missing in Transaction
When executing operations within a multi-document transaction, all targeted collections must exist prior to the transaction's initiation.
🛠️

Solutions

3 solutions available

1. Explicitly Create the Collection easy

The most direct solution is to create the collection before attempting to insert data into it.

1
Connect to your MongoDB instance using the mongo shell or a MongoDB client.
2
Select the database where you want to create the collection.
use your_database_name
3
Create the collection explicitly using the `createCollection` command.
db.createCollection('your_collection_name')
4
Now, attempt your insert operation again. It should succeed.
db.your_collection_name.insertOne({ key: 'value' })

2. Enable Implicit Collection Creation (if appropriate) medium

Temporarily or permanently disable the restriction on implicit collection creation.

1
Locate your MongoDB configuration file. This is typically named `mongod.conf` or `mongod.cfg`.
2
Edit the configuration file to add or modify the `noImplicitCollection` setting.
storage:
  dbPath: /var/lib/mongodb
  collectionOptions:
    noImplicitCollection: false
3
Save the configuration file.
4
Restart the MongoDB `mongod` service for the changes to take effect.
sudo systemctl restart mongod
5
Attempt your insert operation again. Implicit creation should now be allowed.
db.your_collection_name.insertOne({ key: 'value' })

3. Check Application Code for Collection Creation Logic medium

Investigate your application's code to ensure it's not inadvertently triggering this error.

1
Review the code that performs the database operation leading to the error.
2
Look for any database drivers or ORM configurations that might be set to prevent implicit collection creation.
3
If you're using a driver, check its documentation for options related to collection creation. For example, in some Node.js drivers, you might find options to explicitly set collection creation behavior.
Example (conceptual, actual syntax varies by driver):
db.createCollection('your_collection_name', { capped: false, autoIndexId: true });
4
Ensure your application code either explicitly creates the collection before inserting or is configured to allow implicit creation if that's the desired behavior.
🔗

Related Errors

5 related errors