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

Upsert rejected with error if _id assigned to in $setOnInsert and _id equality match specified in query predicate

    • Type: Icon: Improvement Improvement
    • Resolution: Won't Fix
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Write Ops
    • None
    • Query

      Upsert are rejected with an error if the update meets both conditions:

      • _id is assigned to from $setOnInsert
      • an equality match on _id is specified in query predicate

      See the following reproduction:

      > db.foo.update({_id: 0}, {$setOnInsert: {_id: 1}, $set: {x: 1}}, {upsert: true})
      WriteResult({
      	"nMatched" : 0,
      	"nUpserted" : 0,
      	"nModified" : 0,
      	"writeError" : {
      		"code" : 66,
      		"errmsg" : "After applying the update to the document {_id: 0.0 , ...}, the (immutable) field '_id' was found to have been altered to _id: 1.0"
      	}
      })
      

      And, note that this does not generate an error for other fields:

      > db.foo.update({z: 0}, {$setOnInsert: {z: 1}, $set: {x: 1}}, {upsert: true})
      WriteResult({
      	"nMatched" : 0,
      	"nUpserted" : 1,
      	"nModified" : 0,
      	"_id" : ObjectId("552eca4fb15badb258938496")
      })
      > db.foo.find()
      { "_id" : ObjectId("552eca4fb15badb258938496"), "z" : 1, "x" : 1 }
      >
      

            Assignee:
            backlog-server-query Backlog - Query Team (Inactive)
            Reporter:
            rassi J Rassi
            Votes:
            0 Vote for this issue
            Watchers:
            10 Start watching this issue

              Created:
              Updated:
              Resolved: