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

findAndModify with upsert=true generates duplicate key errors

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 4.2.9
    • Component/s: None
    • None
    • Server Triage
    • ALL

      This is very similar to SERVER-14322 however I'm in a case where the auto retry should happen from what I understand but it does not.

       

      Error:

      E11000 duplicate key error collection: dbName.collectionName index: a_1_b_1 dup key: { a: 31910, b: "2021-09-02T00:00:00Z" }
      

      Indexes

       

      > db.collectionName.getIndexes()
      [
      	{
      		"v" : 2,
      		"key" : {
      			"_id" : 1
      		},
      		"name" : "_id_",
      		"ns" : "databaseName.collectionName"
      	},
      	{
      		"v" : 2,
      		"unique" : true,
      		"key" : {
      			"a" : 1,
      			"b" : 1
      		},
      		"name" : "a_1_b_1",
      		"ns" : "databaseName.collectionName",
      		"sparse" : true
      	}
      ]
      

       

       

      Commands run (though ReactiveMongo Scala):

      > db.collectionName.findAndModify(
        query: { a: 31910, b: "2021-09-02T00:00:00Z" },
        update: { $inc: { c: 1 } },
        new: true
        upsert: true
      )
      

       

      In our use case, 8 commands are sent at the same time and sometimes we get 1 such error (not always).

       

      Current document matching:

      > db.collectionName.find({a:31910, b: "2021-09-02T00:00:00Z"})
      { "_id" : ObjectId("613062a3a00a6ac1f7687e5e"), "b" : "2021-09-02T00:00:00Z", "a" : 31910, "c" : 389723 }
      

      Is this expected? Am I missing something?

      We can add some retry mechanism in our code but I'd like to understand why the auto retry does not work.

       

      Any comment will be appreciated.

            Assignee:
            backlog-server-triage [HELP ONLY] Backlog - Triage Team
            Reporter:
            gjourdanweil@gmail.com Gaël Jourdan-Weil
            Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved: