[SERVER-15801] Cannot $inc positional array while performing $setOnInsert operation Created: 24/Oct/14  Updated: 27/Oct/14  Resolved: 27/Oct/14

Status: Closed
Project: Core Server
Component/s: Write Ops
Affects Version/s: 2.6.5, 2.7.7
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Norberto Fernando Rocha Leite (Inactive) Assignee: Unassigned
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-10711 $setOnInsert mods should not conflict... Closed
Operating System: ALL
Participants:

 Description   

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



 Comments   
Comment by Norberto Fernando Rocha Leite (Inactive) [ 27/Oct/14 ]

Duplicated

Generated at Thu Feb 08 03:39:01 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.