What is the difference between updateOne, updateMany, and replaceOne in MongoDB?
Understand the difference between MongoDB updateOne, updateMany, and replaceOne, when each applies, and how replaceOne overwrites whole documents.
Expected Interview Answer
updateOne modifies the first document matching a filter using update operators, updateMany modifies every matching document using update operators, and replaceOne swaps the entire matched document (except its _id) for a brand-new document with no update operators.
updateOne and updateMany both expect an update document built from operators like $set, $inc, or $push, and they change only the fields you name while leaving the rest intact. replaceOne instead takes a plain document and overwrites all fields, so any field you omit is dropped. All three accept the upsert option to insert when nothing matches, and all return a result describing matched and modified counts.
- updateOne targets a single record precisely by the first match
- updateMany applies one change across a whole set of documents
- replaceOne cleanly overwrites a document's full shape
- Update operators preserve untouched fields and avoid accidental data loss
- The upsert option unifies insert-or-update logic in one call
AI Mentor Explanation
updateOne is like correcting one batter's score on the scorecard, updateMany is like adding a penalty run to every batter's tally after a rule breach, and replaceOne is like tearing out a player's whole record card and pinning up a freshly written one. The first two edit specific figures; the last swaps the entire card while keeping the same player slot.
Step-by-Step Explanation
Step 1
Choose the scope
Decide whether you need to change one document (updateOne) or all matches (updateMany).
Step 2
Pick partial vs full change
Use update operators for partial edits; use replaceOne to overwrite the whole document.
Step 3
Write the filter
Build a query document that selects the target record or records.
Step 4
Build the update
For update* pass operators like $set/$inc; for replaceOne pass a plain replacement document without operators.
Step 5
Consider upsert
Add { upsert: true } when you want an insert if nothing matches.
Step 6
Inspect the result
Check matchedCount, modifiedCount, and upsertedId to confirm what happened.
What Interviewer Expects
- Clear distinction between operator updates and full replacement
- Knowing updateOne hits only the first match
- Knowing replaceOne drops omitted fields but keeps _id
- Awareness of the upsert option on all three
- Understanding of matchedCount vs modifiedCount
Common Mistakes
- Passing update operators to replaceOne (it rejects them)
- Passing a plain document to updateOne without operators
- Expecting updateOne to change all matches
- Forgetting that replaceOne removes fields you did not include
- Assuming _id can be changed by replaceOne
Best Answer (HR Friendly)
“updateOne changes the first matching record, updateMany changes every matching record, and both edit only the fields you specify. replaceOne is different — it throws away the whole record and puts a completely new one in its place, keeping only the original ID.”
Code Example
const users = db.collection('users');
// Edit one field on the first matching document
await users.updateOne(
{ _id: 1 },
{ $set: { status: 'active' } }
);
// Edit a field on every matching document
await users.updateMany(
{ plan: 'free' },
{ $inc: { loginCount: 1 } }
);
// Overwrite the whole document (omitted fields are dropped)
await users.replaceOne(
{ _id: 1 },
{ name: 'Alice', status: 'active' }
);Follow-up Questions
- What happens to fields you omit when using replaceOne?
- How does the upsert option behave across these three methods?
- What is the difference between matchedCount and modifiedCount?
- Can replaceOne change the _id of a document?
- How would you update all matching documents but only the first with a different value?
MCQ Practice
1. Which method overwrites the entire document except its _id?
replaceOne swaps the whole matched document for a new one, preserving only the _id.
2. What does updateOne do when several documents match the filter?
updateOne modifies only the first document that matches the filter.
3. What happens if you pass update operators like $set to replaceOne?
replaceOne expects a plain replacement document; passing update operators raises an error.
Flash Cards
updateOne vs updateMany? — updateOne changes the first match; updateMany changes every match. Both use operators.
What does replaceOne keep from the old document? — Only the _id; every other field is overwritten by the new document.
Which methods accept the upsert option? — All three: updateOne, updateMany, and replaceOne.
matchedCount vs modifiedCount? — matchedCount is how many documents the filter found; modifiedCount is how many actually changed.