MongoDB Replace Documents Using MongoShell (original) (raw)

Last Updated : 5 May, 2026

replaceOne() replaces an entire matching document with a new one in a single operation. Replaces the full document, not specific fields

**Syntax:

db.collection.replaceOne(, , {upsert: , writeConcern: ,
collation: , hint: <document|string>})

Return value of replaceOne()

By default, replaceOne() returns an object that contains:

Examples of MongoDB replaceOne()

In the following examples, we are working with:

Screenshot-2026-02-04-171030

Example 1: Replace the First Document

Replaces the first matching employee document with a new one using replaceOne().

**Query:

db.collection.replaceOne({}, {name: "Clevin", age: 27, branch: "EEE", department: "HR", joiningYear: 2024})

**Output:

Screenshot-2026-02-04-171535

Example 2: Replacing single document that matches the filter

Replacing single document that matches the filter

**Query:

db.employee.replaceOne({ name: "Maria" }, { name: "Maria", age: 25, branch: "CSE", department: "Designing" })

**Output:

Screenshot-2026-02-04-172231