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

$setOnInsert executes javascript dispite lack of insertion

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 3.4.10, 3.4.15
    • Component/s: JavaScript, Usability, Write Ops
    • Labels:
      None
    • Query
    • ALL
    • 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  

      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.

            Assignee:
            backlog-server-query Backlog - Query Team (Inactive)
            Reporter:
            jackluo923 Jack Luo
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: