[SERVER-10685] $setOnInsert prevents setting the _id Created: 05/Sep/13  Updated: 06/Mar/14  Resolved: 09/Sep/13

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

Type: Bug Priority: Major - P3
Reporter: Wilfred Springer Assignee: Unassigned
Resolution: Duplicate Votes: 0
Labels: upsert
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-9958 $setOnInsert should allow _id to be set Closed
Related
is related to SERVER-6399 Refactor update() code Closed
Operating System: ALL
Participants:

 Description   

I need to insert a new document if it doesn't exist yet, or return an existing document if it already exists.

db.projects.findAndModify({
    query: {parentProjectId: "project:522755ba0d8686aa6f356393", name: "Eisner" },
    update: { $setOnInsert: {_id: "project:79792889929982797297972797222"} },
    upsert: true
})

However Mongo fails with an error:

	"errmsg" : "exception: Mod on _id not allowed",
	"code" : 10148,
	"ok" : 0

I can see why this would be a problem with a $set but not why it would have to fail with a $setOnInsert. The document that is about to be inserted hasn't been inserted yet.


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