MongoDB updateOne() Method – db.Collection.updateOne()
In MongoDB, updateOne() method updates a first matched document within the collection based on the given query. When you update your document the value of the _id field remains unchanged. This method updates one document at a time and can also add new fields in the given document. It takes three parameters, the first one is the selection criteria to update the document, the second one is the new data to be updated, and the remaining are optional.
- This method can accept a document that only holds update operator expressions.
- This method can also accept aggregation pipeline.
- 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.
Syntax:
db.Collection_name.updateOne(
{Selection_Criteria}, {$set:{Update_data}},
{
upsert: <boolean>,
writeConcern: <document>,
collation: <document>,
arrayFilters: [<filterdocument1>, … ],
hint: <document|string>
})
Parameters:
- The first parameter is the Older value in the form of Documents. Documents are a structure created of file and value pairs, similar to JSON objects.
- The second parameter must contain a $set keyword to update the following specify document value.
- The third parameter is optional.
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.
Examples:
In the following examples, we are working with:
Database: gfg
Collection: student
Document: Four documents contains name and age of the students
Example 1: Update the age of the student whose name is Annu
db.student.updateOne({name: "Annu"}, {$set:{age:25}})
Here, the first parameter is the document whose value to be changed {name:”Annu”} and the second parameter is set keyword means to set(update) the following first matched key value with the older key value, i.e., from 20 to 24.
Example 2: Update the name of the first matched document whose name is Bhannu to Babita
db.student.updateOne({name:"Bhannu"},{$set:{name:"Babita"}})
Here, the first parameter is the document whose value to be changed {name:”Bhannu”} and the second parameter is set keyword means to set(update) the following first matched key value with the older key value.
Note: Here, the value of the key must be of the same datatype that was defined in the collection.
Example 3: Insert a new field in the document using the updateOne method
db.student.updateOne({name: "Bhannu"}, {$set:{class: 3}})
Here, a new field is added, i.e., class: 3 in the document of a student whose name is Bhannu.
Please Login to comment...