$set

Reference type:Cheat sheetFormat:Syntax + example + use case

Reference group: Update operator families

What it does

Sets a field without replacing the whole document.

Syntax

{$set: { field: value }}

Use case

Update selected properties.

$set: practical example
db.users.updateOne({ _id: userId }, { $set: { "profile.theme": "dark" } })
💡 Explanation Sets a field without replacing the whole document. When to use it Update selected properties.
Key Takeaways
  • Sets a field without replacing the whole document.
  • Typical use: Update selected properties.