querydrill

Learn ›MongoDB fundamentals ›Updating documents

updateOne vs updateMany

Obvious once stated, and still asked in interviews - usually to see whether you reach for the safe one by default.

updateOne()

Updates at most one matching document.

updateMany()

Updates every matching document.

Try it

Six users are pending in the sample data:

db.users.updateMany(
  { status: "pending" },
  {
    $set: { status: "active" }
  }
)

modifiedCount: 6. Change it to updateOne and the same filter gives you 1.