QuickDevGuide

Home › MongoDB

Update Operators & Arrays — MongoDB

Updates take a filter and an update document with operators. A replacement document (no operators) overwrites the whole document — use replaceOne for that on purpose. Operator Effect $set / $unset set / remove fields $inc, $mul atomic arithmetic $min / $max update only if lower / higher $rename, $currentDate rename field, set timestamp $push (+ $each, $slice, $sort) append to array (capped) $addToSet append if not present $pull / $pop remove matching / first-last element $, $[], $[elem] positional: first match, all, filtered Options { upsert: true } — insert when nothing matches. findOneAndUpdate(..., { returnDocument: "after" }) — update and return the document atomically (counters, job queues). Single-document updates are atomic, including embedded arrays.

Key Takeaways