Typorithm · medium

Update Documents

mongo-updatemongodbupdate$setupsert
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Update Documents

0%

CRUD

updateOne edits the first match; updateMany edits all matches. Use $set to change specific fields without replacing the whole document; add upsert: true to insert when nothing matches.

Updates take a filter plus an update document. updateOne touches the first match; updateMany touches every match.

Always use update operators like $set: { $set: { age: 37 } } changes only the age field, leaving everything else untouched.

Passing a plain object without operators would replace the whole document; add { upsert: true } as a third argument to insert a new document when the filter matches nothing.

01

updateOne edits the first match.

02

updateMany edits all matches.

03

$set changes fields without replacing the doc.

db.users.updateOne(
  { name: "Ada" },
  { $set: { age: 37 } },
)
db.users.updateMany({ active: false }, { $set: { active: true } })

Read the implementation

Connect each part of the code to the concept before you type.

1. Update one

updateOne modifies the first document matching the filter.

db.users.updateOne(

2. $set operator

$set changes only the named fields, leaving the rest of the document intact.

{ $set: { age: 37 } }

3. Update many

updateMany applies the update to every matching document.

db.users.updateMany({ active: false }, { $set: { active: true } })

4. Match filter

The first argument is the filter selecting which documents to update.

{ active: false }

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Update Documents: Lesson & Practice · Typorithm | ApplyDjinn