Suggest an editImprove this articleRefine the answer for “How do you update a document in the DB?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)To update a document in MongoDB, use `updateOne()` or `updateMany()` - depending on whether one document or several need to change: the first parameter is the search filter, the second is an operator like `$set` that specifies which fields to update. **Key point:** if the entire document needs to be replaced rather than just individual fields, `replaceOne()` is used instead.Shown above the full answer for quick recall.Answer (EN)ImageTo update a document in MongoDB, use `updateOne()` or `updateMany()` - depending on whether one document or several need to change. ### An example of updating a single document: ```js db.users.updateOne( { name: "Alex" }, // the search condition { $set: { age: 26 } } // which fields to change ) ``` ### How this works: - the first parameter is the filter MongoDB uses to find the right document - the second parameter is the `$set` operator, specifying which fields to update ### If several documents need to be updated: ```js db.users.updateMany( { isActive: true }, { $set: { verified: true } } ) ``` ### And if the whole document needs to be replaced: ```js db.users.replaceOne( { name: "Alex" }, { name: "Alex", age: 26, verified: true } ) ``` In short: documents are updated via `updateOne()`, `updateMany()`, or `replaceOne()`, using operators like `$set`.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.