MongoDB updateOne() Method Syntax

db.collection.updateOne(<filter>, <update>, {
   upsert: <boolean>,
   writeConcern: <document>,
   collation: <document>,
   arrayFilters: [<filterdocument1>, ...],
   hint: <document|string> // Available starting in MongoDB 4.2.1
})

Parameters:

  • <filter>: Specifies the selection criteria for the update.
  • <update>: A document or pipeline containing modifications to apply.

Optional Parameters:

  • upsert: The default value of this parameter is false. When it is true it will make a new document in the collection when no document matches the given condition in the update method.
  • writeConcern: It is only used when you do not want to use the default write concern. The type of this parameter is a document.
  • collation: It specifies the use of the collation for operations. It allows users to specify the language-specific rules for string comparison like rules for lettercase and accent marks. The type of this parameter is a document.
  • arrayFilters: It is an array of filter documents that indicates which array elements to modify for an update operation on an array field. The type of this parameter is an array.
  • hint: It is a document or field that specifies the index to use to support the filter. It can take an index specification document or the index name string and if you specify an index that does not exist, then it will give an error.

Return:

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.

MongoDB – updateOne() Method

The updateOne() method in MongoDB updates the first matched document within the collection based on the given query.

The value of the _id field remains unchanged after updating the value. This method updates one document at a time and can also add new fields to the given document.

Important Points:

  • This method can accept a document that only holds update operator expressions.
  • This method can also accept aggregation pipelines.
  • In this method, if the value of upsert is set to true for the shard collection, then you must include the full shard key in the filter/selection criteria. Or if the value of upsert is not set to true, then you must include an exact match on the _id field.
  • The update operation will fail if this operation changes the size of the document.
  • You can also use this method inside multi-document transactions.

Similar Reads

MongoDB updateOne() Method Syntax

db.collection.updateOne(, , {   upsert: ,   writeConcern: ,   collation: ,   arrayFilters: [, ...],   hint: // Available starting in MongoDB 4.2.1})...

MongoDB updateOne Method Examples

In the following examples, we are working with:...

Contact Us