[SERVER-38729] Make $setOnInsert work with upserts and positional array updates Created: 20/Dec/18  Updated: 04/Jan/19  Resolved: 04/Jan/19

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Alexander Komyagin Assignee: Asya Kamsky
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-3326 Support $ positional operator with an... Closed
Participants:

 Description   

Right now when doing an upsert with an update of a specific array element, MongoDB will create a new document with an "embedded doc":

db.m1.update({"_id.reportingPeriod":new Date("2018-12-02T00:00:00Z")},{$set:{"daily_stats.2":10},},{upsert:true})

I think it will be beneficial for users to be able to tell MongoDB that the "daily_stats" field should be an array when creating a new document. Intuitively, $setOnInsert could do the trick instead of complaining of the conflict:

> db.m1.update({"_id.reportingPeriod":new Date("2018-12-02T00:00:00Z")},{$set:{"daily_stats.2":10}, $setOnInsert:{daily_stats:[]}},{upsert:true})
WriteResult({
	"nMatched" : 0,
	"nUpserted" : 0,
	"nModified" : 0,
	"writeError" : {
		"code" : 40,
		"errmsg" : "Updating the path 'daily_stats' would create a conflict at 'daily_stats'"
	}
})



 Comments   
Comment by Asya Kamsky [ 04/Jan/19 ]

alex.komyagin this looks identical to SERVER-3326 at least via title. Your example doesn't seem to use positional operator but I think the idea/concept is the same, you want result to be an array and there is no language to specify it.

Comment by Craig Homa [ 03/Jan/19 ]

Asya to determine if this is a dupe ticket.

Generated at Thu Feb 08 04:49:49 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.