The Wayback Machine - https://web.archive.org/web/20260228025418/https://www.geeksforgeeks.org/mongodb/mongodb-updateone-method-db-collection-updateone/

MongoDB - updateOne() Method

Last Updated : 4 Feb, 2026

The updateOne() method in MongoDB updates the first document that matches a specified condition in a collection. It is used to change fields or values in a single matching document without affecting others.

Syntax

db.collection.updateOne(<filter>, <update>, {
   upsert: <boolean>,
   writeConcern: <document>,
   collation: <document>,
   arrayFilters: [<filterdocument1>, ...],
   hint: <document|string> // Available starting in MongoDB 4.2.1
})
  • <filter>: Criteria to select the document to update.
  • <update>: Modifications to apply (document or pipeline).
  • upsert: false by default; true inserts a new document if none match.
  • writeConcern: Overrides the default write concern.
  • collation: Language-specific rules for string comparison.
  • arrayFilters: Filters specifying which array elements to update.
  • hint: Specifies which index to use for the operation.

Return value of updateOne()

This method returns a document that contains the following fields:

  • nMatched: This field contains the number of matched documents.
  • modifiedCount: This field contains the number of modified documents.
  • upsertedId: This field contains the _id for the upserted document.
  • acknowledged: The value of this field is true if write concern was enabled or false if write concern was disabled.

Examples of MongoDB updateOne() Method

Let’s go over a few examples to understand how updateOne() works in MongoDB. In the following examples, we are working with:

  • Database: gfg
  • Collection: student
  • Document: Three documents contains name and age of the students
Screenshot-2026-02-04-131707

Example 1: Update an Integer Value in the Document

Update the age of the student whose name is Alen.

Query:

db.student.updateOne({Name: "Alen"}, {$set:{age:25}})

Output:


Screenshot-2026-02-04-131923

Example 2: Update a String Value in the Document

Update the first matched name from Ron to Ryan (same data type). Here, the value of the key must be of the same data type that was defined in the collection.

Query:

db.student.updateOne({Name:"Ron"},{$set:{Name:"Ryan"}})

Output:

Screenshot-2026-02-04-143347

Example 3: Insert a new field in the document

Add a new field named class with the value 3 to the document where the student’s name is Kim.

Query:

db.student.updateOne({name: "Kim"}, {$set:{class: 3}})

Output:

Screenshot-2026-02-04-143909

Example 4: Update using Update Operator Expressions

Updating all documents where name is "Kim" to set age to 25:

Query:

db.student.updateMany({ name: "Kim" }, { $set: { age: 25 } })

Output:

Screenshot-2026-02-04-144421

Example 5: Update with Aggregation Pipeline

Incrementing the age of all documents by 1 where name is "Kim" using the aggregation pipeline.

Query:

db.student.updateMany({ name: "Kim" }, [{ $set: { age: { $add: ["$age", 1] } } }])

Output:

Screenshot-2026-02-04-144647

Example 6: Update with Upsert

Updating the document with name "Clevin" if it exists; otherwise, inserting a new document with name "Clevin" and age 28.

Query:

db.student.updateOne({ name: "Clevin" }, { $set: { age: 28 } }, { upsert: true })

Output:

Screenshot-2026-02-04-144954
Comment

Explore