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

Cannot $inc positional array while performing $setOnInsert operation

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.6.5, 2.7.7
    • Component/s: Write Ops
    • None
    • ALL

      If one tries to $setOnInsert an array or increment a positional (or several) elements of that array if that document already exists, the instruction fails. Depending on the order by which they are placed on the update statement it resolves 2 different error messages:

      • $setOnInsert before $inc
        db.t.update({_id:'12345',hour:12345},{ $setOnInsert: { rpm: [10, 15] }, $inc: { count: 1, 'rpm.$0': 10, 'rpm.$1': 15 }}, { upsert:true })
        WriteResult({
        	"nMatched" : 0,
        	"nUpserted" : 0,
        	"nModified" : 0,
        	"writeError" : {
        		"code" : 16836,
        		"errmsg" : "cannot use the part (rpm of rpm.$0) to traverse the element ({rpm: [ 10.0, 15.0 ]})"
        	}
        })
        
      • $seOnInsert after $inc
        db.t.update({_id:'12345',hour:12345},{ $inc: { count: 1, 'rpm.$0': 10, 'rpm.$1': 15 }, $setOnInsert: { rpm: [10, 15] }}, { upsert:true })
        WriteResult({
        	"nMatched" : 0,
        	"nUpserted" : 0,
        	"nModified" : 0,
        	"writeError" : {
        		"code" : 16836,
        		"errmsg" : "Cannot update 'rpm.$0' and 'rpm' at the same time"
        	}
        })
        

      In either situation this operation should be valid since it's very common, specially for time series operations/use cases, where we need to preallocate arrays and increment counters if those already exist.

      The code reveals that we do not treat this valid situation:
      https://github.com/mongodb/mongo/blob/master/src/mongo/db/ops/path_support.cpp#L110

      https://github.com/mongodb/mongo/blob/master/src/mongo/db/ops/update_driver.cpp#L270-L278

            Assignee:
            Unassigned Unassigned
            Reporter:
            norberto.leite Norberto Fernando Rocha Leite (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: