# MongoServerError: 集合已存在。createCollection 命令中的选项与现有集合选项冲突

- **ID:** `database/mongodb-cannot-create-collection-options-conflict`
- **领域:** database
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

同名的集合已存在，但其选项（例如，不同的排序规则、验证规则或存储引擎设置）与 createCollection 命令中指定的选项不同。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| MongoDB 5.0 | active | — | — |
| MongoDB 6.0 | active | — | — |
| MongoDB 7.0 | active | — | — |
| MongoDB 8.0 | active | — | — |

## 解决方案

1. ```
   使用 `db.getCollectionInfos({name: 'mycollection'})` 检查现有集合选项。如果选项可接受，直接使用现有集合而不修改。如果不行，重命名旧集合：`db.mycollection.renameCollection('mycollection_old')` 然后使用所需选项创建新集合：`db.createCollection('mycollection', {collation: {locale: 'en', strength: 2}, validator: {email: {$type: 'string'}}})`
   ```
2. ```
   使用 collMod 修改兼容选项：`db.runCommand({collMod: 'mycollection', validator: {email: {$type: 'string'}}, validationLevel: 'strict'})`。对于排序规则，必须删除并重新创建集合。
   ```

## 无效尝试

- **Drop the collection and recreate it with the desired options** — Dropping the collection will delete all data permanently; this is irreversible unless you have a backup (30% 失败率)
- **Ignore the error and use the collection as-is** — If the existing collection has different options (e.g., missing a required validator), your application may behave incorrectly or fail to enforce data integrity (60% 失败率)
- **Use the collMod command to change options on the existing collection** — collMod can only modify certain options like validation rules and indexes, but cannot change fundamental options like collation or storage engine (50% 失败率)
