20 mongodb runtime_error ai_generated true

MongoServerError: 无法从固定集合中删除文档

MongoServerError: cannot delete from a capped collection

ID: mongodb/capped-collection-deletion

其他格式: JSON · Markdown 中文 · English
92%修复率
88%置信度
1证据数
2023-06-15首次发现

版本兼容性

版本状态引入弃用备注
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

根因分析

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

English

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

generic

官方文档

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

解决方案

  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 });`

无效尝试

常见但无效的做法:

  1. 15% 失败

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

  2. 10% 失败

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

  3. 5% 失败

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