Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-88309

Prevent user from inserting doc via applyOps with recordId that already exists

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None
    • Storage Execution
    • ALL
    • Execution Team 2024-04-01

      If you have documents and an index {a: 1}:

      // Collection:
      $recordId: 1 -> {_id: 1, a: 1}
      $recordId: 2 -> {_id: 2 a: 2}
      $recordId: 3 -> {_id: 3, a: 3}
      
      // {a: 1} index:
      {a: 1} -> $recordId: 1
      {a: 2} -> $recordId: 2
      {a: 3} -> $recordId: 3
      

      Perform an insert via applyOps:

      db.runCommand({
          applyOps: [
          {
            "op" : "i",
            "ns" : "test.mycoll",
            "o" : {
              "_id" : 34,
              "a": 34
            },
            "o2" : {
              "_id" : 34,
            },
            "rid": NumberLong(1)
          }
        ]
      })
      

      In the collection, we end up overwriting the document. But in the index, we add a new entry!

      // Collection:
      $recordId: 1 -> {_id: 34, a: 34}
      $recordId: 2 -> {_id: 2 a: 2}
      $recordId: 3 -> {_id: 3, a: 3}
      
      // {a: 1} index:
      {a: 1} -> $recordId: 1
      {a: 2} -> $recordId: 2
      {a: 3} -> $recordId: 3
      {a: 34} -> $recordId: 1 // new entry
      

      And if you delete the document with {a: 34}, you'll have a dangling index entry:

      // Collection:
      $recordId: 2 -> {_id: 2 a: 2}
      $recordId: 3 -> {_id: 3, a: 3}
      
      // {a: 1} index:
      {a: 1} -> $recordId: 1 // dangling
      {a: 2} -> $recordId: 2
      {a: 3} -> $recordId: 3
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            vishnu.kaushik@mongodb.com Vishnu Kaushik
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: