# MongoServerError: Cannot create index 'idx_email_unique': a unique index on field 'email' already exists with different options

- **ID:** `database/mongodb-cannot-create-index-duplicate-key`
- **Domain:** database
- **Category:** config_error
- **Verification:** ai_generated
- **Fix Rate:** 90%

## Root Cause

An existing unique index on the same field has different options (e.g., collation, sparse, partial filter expression) than the new index being created, causing a conflict.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| MongoDB 5.0 | active | — | — |
| MongoDB 6.0 | active | — | — |
| MongoDB 7.0 | active | — | — |
| MongoDB 8.0 | active | — | — |

## Workarounds

1. **List existing indexes on the collection: `db.collection.getIndexes()`. Identify the conflicting index and drop it: `db.collection.dropIndex('idx_email_unique')`. Then create the new index with the desired options: `db.collection.createIndex({email: 1}, {unique: true, name: 'idx_email_unique', collation: {locale: 'en', strength: 2}})`** (90% success)
   ```
   List existing indexes on the collection: `db.collection.getIndexes()`. Identify the conflicting index and drop it: `db.collection.dropIndex('idx_email_unique')`. Then create the new index with the desired options: `db.collection.createIndex({email: 1}, {unique: true, name: 'idx_email_unique', collation: {locale: 'en', strength: 2}})`
   ```
2. **If the existing index has different options but you want to keep it, rename the new index to a different name: `db.collection.createIndex({email: 1}, {unique: true, name: 'idx_email_unique_v2'})`** (85% success)
   ```
   If the existing index has different options but you want to keep it, rename the new index to a different name: `db.collection.createIndex({email: 1}, {unique: true, name: 'idx_email_unique_v2'})`
   ```

## Dead Ends

- **Drop the existing index with db.collection.dropIndex() and recreate it with the same name but different options** — If the existing index is being used by an application, dropping it may cause query performance degradation or application errors until the new index is created (50% fail)
- **Create the index with the force: true option** — MongoDB does not support a 'force' option for index creation; this will result in a syntax error (100% fail)
- **Ignore the error and assume the index already exists** — The existing index may have different collation or options, leading to unexpected behavior in queries or uniqueness enforcement (60% fail)
