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

$setOnInsert executes javascript dispite lack of insertion

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor - P4
    • Resolution: Works as Designed
    • Affects Version/s: 3.4.10, 3.4.15
    • Fix Version/s: None
    • Component/s: JavaScript, Usability, Write Ops
    • Labels:
      None
    • Operating System:
      ALL
    • Steps To Reproduce:
      Hide

       

      db.system.js.save({
       _id: "nextId", 
       value: function (x) {
          return db.counters.findAndModify({
            query:{_id:x}, 
            update:{$inc:{value:1}}, 
            new:true
          }).value;
       }
      })
      db.loadServerScripts()  // make the nextId available directly
      db.counters.insert({_id: "_id", value: 0})
      db.counters.find({})     // shows that counter is 0
      db.test.update(
          {"value": "abc"},
          {$setOnInsert: {"_id": nextId("_id"), "value": "abc"}},
          {upsert: true}
      )
      db.counters.find({}) // previous update upserted and shows that counter is incremented to 1
      db.test.update(
          {"value": "abc"},
          {$setOnInsert: {"_id": nextId("_id"), "value": "abc"}},
          {upsert: true}
      )
      db.counters.find({}) // previous update does not modify document, however counter is incremented to 2
      

       

      Show
        db.system.js.save({ _id: "nextId" , value: function (x) { return db.counters.findAndModify({ query:{_id:x}, update:{$inc:{value: 1 }}, new : true }).value; } }) db.loadServerScripts() // make the nextId available directly db.counters.insert({_id: "_id" , value: 0 }) db.counters.find({}) // shows that counter is 0 db.test.update( { "value" : "abc" }, {$setOnInsert: { "_id" : nextId( "_id" ), "value" : "abc" }}, {upsert: true } ) db.counters.find({}) // previous update upserted and shows that counter is incremented to 1 db.test.update( { "value" : "abc" }, {$setOnInsert: { "_id" : nextId( "_id" ), "value" : "abc" }}, {upsert: true } ) db.counters.find({}) // previous update does not modify document, however counter is incremented to 2  

      Description

      https://docs.mongodb.com/manual/reference/operator/update/setOnInsert/

      According to the documentation, "If the update operation does not result in an insert,$setOnInsert does nothing." 

      This statement is not true for all cases. The documentation should be changed to "$setOnInsert does insert values" or equivalent rather than "$setOnIN=nsert does nothing". In the case where javascript function is specified inside $setOnInsert clause, the javascript function is still executed dispite no document insertion.

      Reproduction steps is verified on mongodb 3.4.10 and 3.4.15.

        Attachments

          Activity

            People

            • Votes:
              0 Vote for this issue
              Watchers:
              7 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: