# Upserts and Replacing a Whole Document

An upsert updates a document when the filter finds one and inserts it when it doesn't, $setOnInsert sets values only on that first insert, replaceOne swaps in a whole new version, and findOneAndUpdate changes a document and hands it back.

![Cheat sheet: Upserts and Replacing a Whole Document](https://fsdtrainingmedia.blob.core.windows.net/public/images/2026/11/a07b064362d84f17a945b50fa82510e1.webp)

- Category: MongoDB
- Level: Journeyman
- Tags: $setOnInsert, findOneAndUpdate, mongodb, replaceOne, Upsert
- Author: Gregor Dzierzon
- Updated: November 23, 2026

Source: https://www.ask-codey.com/cheat-sheets/mongodb-upserts-and-replaceone-cheat-sheet
