updateOne()

Updates the first document that matches the filter using update operators like $set, $inc, $push.

Syntax

mongodb
db.collection.updateOne(filter, update, options)

Example

mongodb
await db.users.updateOne(
  { _id: userId },
  {
    $set: { lastLogin: new Date() },
    $inc: { loginCount: 1 }
  }
);

// Upsert — insert if not found:
await db.settings.updateOne(
  { userId },
  { $setOnInsert: { theme: "dark" } },
  { upsert: true }
);