{ "_comment": [ "Inputs only. Expectations are measured -- see record.js.", "`$`, the original positional operator, which writes the *first* element", "the query matched. It is the one of the three that needs something the", "document alone does not hold -- which element the filter matched -- so", "every case here pairs a filter with an update and the pairing is the", "point. The pinned crud corpus has no `$` case anywhere." ], "documents": [ { "_id": 1, "y": [{ "b": 3 }, { "b": 1 }] }, { "_id": 2, "y": [{ "b": 0 }, { "b": 1 }] }, { "_id": 3, "y": [{ "b": 5, "c": [{ "d": 2 }] }] } ], "cases": [ { "description": "$ writes the element the filter matched", "operation": "updateOne", "arguments": { "filter": { "y.b": 3 }, "update": { "$set": { "y.$.b": 7 } } } }, { "description": "$ writes only the first match when several elements qualify", "documents": [{ "_id": 1, "y": [{ "b": 1 }, { "b": 1 }, { "b": 2 }] }], "operation": "updateOne", "arguments": { "filter": { "y.b": 1 }, "update": { "$set": { "y.$.b": 7 } } } }, { "description": "$ when the filter never touched the array is refused", "operation": "updateOne", "arguments": { "filter": { "_id": 1 }, "update": { "$set": { "y.$.b": 7 } } } }, { "description": "$inc through $", "operation": "updateOne", "arguments": { "filter": { "y.b": 3 }, "update": { "$inc": { "y.$.b": 10 } } } }, { "description": "$ as the leaf replaces the matched element", "operation": "updateOne", "arguments": { "filter": { "y.b": 3 }, "update": { "$set": { "y.$": { "replaced": true } } } } }, { "description": "$ reaching a field below the matched element", "operation": "updateOne", "arguments": { "filter": { "y.b": 5 }, "update": { "$set": { "y.$.c": [] } } } }, { "description": "$ with the filter matching on a different field of the element", "documents": [{ "_id": 1, "y": [{ "k": "a", "v": 1 }, { "k": "b", "v": 2 }] }], "operation": "updateOne", "arguments": { "filter": { "y.k": "b" }, "update": { "$set": { "y.$.v": 9 } } } }, { "description": "updateMany with $ writes each document's own match", "operation": "updateMany", "arguments": { "filter": { "y.b": 1 }, "update": { "$set": { "y.$.b": 7 } } } }, { "description": "$ over an array of scalars", "documents": [{ "_id": 1, "y": [1, 2, 3] }], "operation": "updateOne", "arguments": { "filter": { "y": 2 }, "update": { "$set": { "y.$": 9 } } } }, { "description": "$ in first position is refused", "operation": "updateOne", "arguments": { "filter": { "y.b": 3 }, "update": { "$set": { "$": 7 } } } }, { "description": "$ twice in one path is refused", "operation": "updateOne", "arguments": { "filter": { "y.b": 5 }, "update": { "$set": { "y.$.c.$.d": 0 } } } }, { "description": "$ on an upsert that inserts", "documents": [], "operation": "updateOne", "arguments": { "filter": { "y.b": 3 }, "update": { "$set": { "y.$.b": 7 } }, "upsert": true } } ] }