20 mongodb runtime_error ai_generated true

MongoServerError: cannot delete from a capped collection

ID: mongodb/capped-collection-deletion

Also available as: JSON · Markdown · 中文
92%Fix Rate
88%Confidence
1Evidence
2023-06-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
mongodb-3.6 active
mongodb-4.0 active
mongodb-4.2 active
mongodb-4.4 active
mongodb-5.0 active
mongodb-6.0 active
mongodb-7.0 active

Root Cause

Attempted to delete a document from a capped collection, which only supports insertion and reading, not deletion or update without replacement.

generic

中文

尝试从固定集合中删除文档,固定集合只支持插入和读取,不支持删除或非替换的更新操作。

Official Documentation

https://www.mongodb.com/docs/manual/core/capped-collections/

Workarounds

  1. 85% success Convert the capped collection to an uncapped collection using `convertToCapped` or create a new uncapped collection and copy data via aggregation with $out. Example: `db.runCommand({ convertToCapped: 'myCollection', size: 100000 });` then drop and recreate as uncapped.
    Convert the capped collection to an uncapped collection using `convertToCapped` or create a new uncapped collection and copy data via aggregation with $out. Example: `db.runCommand({ convertToCapped: 'myCollection', size: 100000 });` then drop and recreate as uncapped.
  2. 95% success Use `db.collection.drop()` to remove the entire capped collection (data loss) and recreate it as a regular collection with `db.createCollection('myCollection')`.
    Use `db.collection.drop()` to remove the entire capped collection (data loss) and recreate it as a regular collection with `db.createCollection('myCollection')`.
  3. 90% success Instead of deleting, set a TTL index or use a capped collection with a max document count to automatically age out old data. Example: `db.createCollection('logs', { capped: true, size: 5242880, max: 5000 });`
    Instead of deleting, set a TTL index or use a capped collection with a max document count to automatically age out old data. Example: `db.createCollection('logs', { capped: true, size: 5242880, max: 5000 });`

中文步骤

  1. Convert the capped collection to an uncapped collection using `convertToCapped` or create a new uncapped collection and copy data via aggregation with $out. Example: `db.runCommand({ convertToCapped: 'myCollection', size: 100000 });` then drop and recreate as uncapped.
  2. Use `db.collection.drop()` to remove the entire capped collection (data loss) and recreate it as a regular collection with `db.createCollection('myCollection')`.
  3. Instead of deleting, set a TTL index or use a capped collection with a max document count to automatically age out old data. Example: `db.createCollection('logs', { capped: true, size: 5242880, max: 5000 });`

Dead Ends

Common approaches that don't work:

  1. 15% fail

    allowDiskUse applies to aggregation pipelines, not to deletion operations on capped collections.

  2. 10% fail

    Dropping the capped collection removes all data permanently; a workaround should preserve data if needed.

  3. 5% fail

    The error is triggered by the operation type, not by the filter; any delete attempt on a capped collection fails.