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

findAndModify.lastErrorObject.updatedExisting always true after and update

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 5.0.6
    • Component/s: None
    • None
    • ALL
    • Hide
      1. Use Mongo 5.0.6
      2. Run the following commmand to insert a document:
        {{> db.runCommand({ "findAndModify" : "collection", "query" : { "_id" : ObjectId("628d26b2f8221751cdd7dcd2") }

        , "update" : { "$set" :

        { "name" : "some name" }

        }, "new": true, "upsert": true })}}
        {{}}

      3. Run the following command to update the document with the same values:{}{{{}> db.runCommand({ "findAndModify" : "collection", "query" : { "_id" : ObjectId("628d26b2f8221751cdd7dcd2") }

        , "update" : { "$set" :

        { "name" : "some name" }

        }, "new": true, "upsert": true }){}}}
        {{}}

      4. Observe the result state:
        {{{}lastErrorObject:
        Unknown macro: { n}

        ,{}}}{}

      Show
      Use Mongo 5.0.6 Run the following commmand to insert a document: {{> db.runCommand({ "findAndModify" : "collection", "query" : { "_id" : ObjectId("628d26b2f8221751cdd7dcd2") } , "update" : { "$set" : { "name" : "some name" } }, "new": true, "upsert": true })}} {{}} Run the following command to update the document with the same values: { } {{{}> db.runCommand({ "findAndModify" : "collection", "query" : { "_id" : ObjectId("628d26b2f8221751cdd7dcd2") } , "update" : { "$set" : { "name" : "some name" } }, "new": true, "upsert": true }){}}} {{}} Observe the result state: {{{}lastErrorObject: Unknown macro: { n} ,{}}}{ }

      I've noticed an issue with findAndModify, where updatedExisting always return true when updating a field with the same value.

      I found this blog post that describe the issue well: https://codehunter.cc/a/mongodb/findandupdate-how-to-check-if-document-was-really-updated

      Although this behavior seems to be a bug, it's in line with the documention: https://www.mongodb.com/docs/manual/reference/command/findAndModify/#lasterrorobject

      However, I would expect updatedExisting to be true if the document was upserted or if any modification was done to the existing document. Similar to how the update command output: https://www.mongodb.com/docs/manual/reference/command/update/#mongodb-data-update.nModified

      Might be related to this other issue: https://jira.mongodb.org/browse/SERVER-12329

            Assignee:
            chris.kelly@mongodb.com Chris Kelly
            Reporter:
            sacadieux@ea.com Samuel Cadieux
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: