The error:
"Operation not permitted on a capped collection"
comes from MongoDB, and it indicates that you're trying to perform an operation that is not allowed on a capped collection.
π What Is a Capped Collection?
A capped collection in MongoDB is a special type of collection with:
-
Fixed size (in bytes or documents)
-
Automatic overwrites of the oldest documents when full
-
Insertion order preservation
-
High performance for insert + read workloads (e.g. logs, telemetry)
π« Operations Not Allowed on Capped Collections
The following operations will trigger this error:
❌ 1. Delete Specific Documents
❌ 2. Update with Document Size Increase
You can't update a document in a capped collection if the updated version is larger than the original.
❌ 3. Remove Documents with remove()
or deleteMany()
Only automatic removal by the capped size is allowed.
❌ 4. FindAndModify that Deletes or Replaces
Can't use findAndModify
to delete or replace documents in a capped collection.
✅ How to Fix or Work Around It
π 1. Avoid Deletes and Size-Increasing Updates
-
Only use in-place updates that don't change document size.
-
Use capped collections only for append-only use cases (like logs).
π 2. Check if the Collection Is Capped
To confirm:
π 3. Convert to a Normal Collection (If Needed)
If you need deletes or flexible updates:
-
Create a new normal collection:
-
Copy data:
-
Drop the old capped collection:
-
Rename:
π‘ When Should You Use a Capped Collection?
Only when:
-
You have append-only data
-
Document size is predictable
-
You need fast inserts and fixed-size storage
e.g., log streams, sensor data, circular buffers