{
  "id": "mongodb/index-duplicate-key-on-sparse",
  "signature": "MongoServerError: E11000 duplicate key error collection: test.users index: email_1 dup key: { email: null }",
  "signature_zh": "MongoServerError: E11000 重复键错误，集合：test.users，索引：email_1，重复键：{ email: null }",
  "regex": "E11000 duplicate key error.*dup key: \\{ [^:]+: null \\}",
  "domain": "mongodb",
  "category": "data_error",
  "subcategory": null,
  "root_cause": "Multiple documents have null or missing values for a field with a unique sparse index, causing a duplicate key error because sparse indexes treat null and missing values as the same key.",
  "root_cause_type": "generic",
  "root_cause_zh": "多个文档在具有唯一稀疏索引的字段上具有空值或缺失值，导致重复键错误，因为稀疏索引将空值和缺失值视为相同的键。",
  "versions": [
    {
      "version": "mongodb-3.6",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "mongodb-4.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "mongodb-4.2",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "mongodb-4.4",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "mongodb-5.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "mongodb-6.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "mongodb-7.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "This removes data integrity guarantees for non-null values, potentially allowing unintended duplicates.",
      "fail_rate": 0.15,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "This is a temporary fix; future inserts with null will cause the same error.",
      "fail_rate": 0.1,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Existing null documents remain, so the error persists until they are also updated.",
      "fail_rate": 0.2,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Use a partial unique index instead of sparse to allow multiple nulls. Example: `db.users.createIndex({email:1},{unique:true,partialFilterExpression:{email:{$exists:true,$ne:null}}})`",
      "success_rate": 0.95,
      "how": "Use a partial unique index instead of sparse to allow multiple nulls. Example: `db.users.createIndex({email:1},{unique:true,partialFilterExpression:{email:{$exists:true,$ne:null}}})`",
      "condition": "",
      "sources": []
    },
    {
      "action": "Update all documents with null email to a unique placeholder value, such as an empty string or a generated UUID. Example: `db.users.updateMany({email:null},{$set:{email:UUID().toString()}})`",
      "success_rate": 0.85,
      "how": "Update all documents with null email to a unique placeholder value, such as an empty string or a generated UUID. Example: `db.users.updateMany({email:null},{$set:{email:UUID().toString()}})`",
      "condition": "",
      "sources": []
    },
    {
      "action": "Drop the sparse index and create a regular unique index, ensuring that null values are replaced with a sentinel value first. Example: `db.users.dropIndex('email_1'); db.users.createIndex({email:1},{unique:true})`",
      "success_rate": 0.8,
      "how": "Drop the sparse index and create a regular unique index, ensuring that null values are replaced with a sentinel value first. Example: `db.users.dropIndex('email_1'); db.users.createIndex({email:1},{unique:true})`",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Use a partial unique index instead of sparse to allow multiple nulls. Example: `db.users.createIndex({email:1},{unique:true,partialFilterExpression:{email:{$exists:true,$ne:null}}})`",
    "Update all documents with null email to a unique placeholder value, such as an empty string or a generated UUID. Example: `db.users.updateMany({email:null},{$set:{email:UUID().toString()}})`",
    "Drop the sparse index and create a regular unique index, ensuring that null values are replaced with a sentinel value first. Example: `db.users.dropIndex('email_1'); db.users.createIndex({email:1},{unique:true})`"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://www.mongodb.com/docs/manual/core/index-sparse/",
  "official_doc_section": null,
  "error_code": "11000",
  "verification_tier": "ai_generated",
  "confidence": 0.9,
  "fix_success_rate": 0.85,
  "resolvable": "true",
  "first_seen": "2023-04-10",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}