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

- **ID:** `mongodb/capped-collection-deletion`
- **领域:** mongodb
- **类别:** runtime_error
- **错误码:** `20`
- **验证级别:** ai_generated
- **修复率:** 92%

## 根因

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

## 版本兼容性

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

## 解决方案

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

## 无效尝试

- **** — allowDiskUse applies to aggregation pipelines, not to deletion operations on capped collections. (15% 失败率)
- **** — Dropping the capped collection removes all data permanently; a workaround should preserve data if needed. (10% 失败率)
- **** — The error is triggered by the operation type, not by the filter; any delete attempt on a capped collection fails. (5% 失败率)
