The error "Operation not permitted on a capped collection" in MongoDB means you're trying to perform an operation that is not allowed on a capped collection — a special type of collection with strict constraints.
๐งพ What Is a Capped Collection?
A capped collection in MongoDB is:
-
Fixed-size (in bytes or number of documents).
-
FIFO (First-In-First-Out) — old documents are automatically overwritten when the size limit is reached.
-
Insert-only — documents are inserted in the order received and are not allowed to be deleted or modified in a way that changes their size.
⚠️ Common Causes of This Error
1. Trying to Delete Documents
Capped collections do not allow deletes:
2. Trying to Update Documents to a Larger Size
You cannot increase the size of a document in a capped collection:
3. Running Unsupported Operations
Some operations like remove()
, certain update()
forms, and findAndModify()
with document resizing are disallowed.
✅ How to Fix It
✅ 1. Avoid Deletes or Size-Increasing Updates
-
Only use inserts or in-place updates that do not increase the document size.
-
Keep document structure fixed.
Example of a safe update:
✅ 2. Use a Non-Capped Collection If You Need Flexibility
If you need to:
-
Delete documents
-
Grow documents
-
Perform flexible queries and updates
Then convert to a normal collection.
You can't un-cap a collection, but you can do this:
✅ 3. Check If Collection Is Capped
Use this command:
Returns true
if the collection is capped.
๐ง Summary Table
Action | Capped Collection Allowed? |
---|---|
Insert | ✅ Yes |
In-place update (no size change) | ✅ Yes |
Update that increases size | ❌ No |
Delete documents | ❌ No |
remove() or deleteOne() | ❌ No |