{ "_comment": [ "Inputs only. Expectations are measured -- see record.js.", "`$setOnInsert`, the only update operator whose meaning depends on which", "branch of an upsert ran. Every case here is therefore a pair: the same", "update against a document that exists and against one that does not." ], "documents": [{ "_id": 1, "k": 1, "a": 100 }], "cases": [ { "description": "$setOnInsert writes when the upsert inserts", "documents": [], "operation": "updateOne", "arguments": { "filter": { "k": 1 }, "update": { "$setOnInsert": { "a": 1 }, "$set": { "b": 2 } }, "upsert": true } }, { "description": "$setOnInsert is ignored when the upsert updates", "operation": "updateOne", "arguments": { "filter": { "k": 1 }, "update": { "$setOnInsert": { "a": 1 }, "$set": { "b": 2 } }, "upsert": true } }, { "description": "$setOnInsert alone on a plain update is a no-op", "operation": "updateOne", "arguments": { "filter": { "k": 1 }, "update": { "$setOnInsert": { "a": 1 } } } }, { "description": "$setOnInsert alone on an insert is the whole document", "documents": [], "operation": "updateOne", "arguments": { "filter": { "k": 7 }, "update": { "$setOnInsert": { "a": 1 } }, "upsert": true } }, { "description": "$setOnInsert writes a dotted path on insert", "documents": [], "operation": "updateOne", "arguments": { "filter": { "k": 1 }, "update": { "$setOnInsert": { "x.y": 5 } }, "upsert": true } }, { "description": "$setOnInsert may not touch _id", "documents": [], "operation": "updateOne", "arguments": { "filter": { "k": 1 }, "update": { "$setOnInsert": { "_id": 9 } }, "upsert": true } }, { "description": "$setOnInsert and $set naming the same field on an insert", "documents": [], "operation": "updateOne", "arguments": { "filter": { "k": 1 }, "update": { "$setOnInsert": { "a": 1 }, "$set": { "a": 2 } }, "upsert": true } }, { "description": "findOneAndUpdate reports the document $setOnInsert built", "documents": [], "operation": "findOneAndUpdate", "arguments": { "filter": { "k": 3 }, "update": { "$setOnInsert": { "a": 1 } }, "upsert": true, "returnDocument": "after" } }, { "description": "updateMany with $setOnInsert on an upsert that inserts", "documents": [], "operation": "updateMany", "arguments": { "filter": { "k": 1 }, "update": { "$setOnInsert": { "a": 1 } }, "upsert": true } } ] }