MongoDB – Update Document

MongoDB – Update Document

MongoDB's update() and save() methods are used to update document into a collection. The update() method updates the values in the existing document while the save() method replaces the existing document with the document passed in save() method.

MongoDB Update() Method

The update() method updates the values in the existing document.

Syntax

The basic syntax of update() method is as follows −

>db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPDATED_DATA)

Example

> db.mydb.update({"_id":"ac3"},{$set:{"price":599}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.mydb.find()
{ "_id" : "ac3", "name" : "AC3 Phone", "brand" : "ACME", "type" : "phone", "price" : 599, "rating" : 3.8, "warranty_years" : 1, "available" : true }
{ "_id" : "ac7", "name" : "AC7 Phone", "brand" : "ACME", "type" : "phone", "price" : 320, "rating" : 4, "warranty_years" : 1, "available" : false }
{ "_id" : "ac9", "name" : "AC9 Phone", "brand" : "ACME", "type" : "phone", "price" : 320, "rating" : 4, "warranty_years" : 1, "available" : false }
{ "_id" : "ac11", "name" : "AC11 Phone", "brand" : "ACME", "type" : "phone", "price" : 340, "rating" : 4, "warranty_years" : 1, "available" : false }
{ "_id" : "ac12", "name" : "AC12 Phone", "brand" : "ACME", "type" : "phone", "price" : 450, "rating" : 4.6, "warranty_years" : 2, "available" : true }
{ "_id" : "ac13", "name" : "AC13 Phone", "brand" : "ACME", "type" : "phone", "price" : 6000, "rating" : 5, "warranty_years" : 2.5, "available" : true }
>

above example will set the new price of ac3 model to 599.

By default, MongoDB will update only a single document. To update multiple documents, you need to set a parameter 'multi' to true.

db.mydb.update({"_id":"ac3"},{$set:{"price":599,"rating":4}},{multi:true})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

## MongoDB Save() Method
The save() method replaces the existing document with the new document passed in the save() method.
### Syntax
The basic syntax of MongoDB save() method is shown below −

db.COLLECTION_NAME.save({_id:ObjectId(),NEW_DATA})

### Example
Following example will replace the document with the _id 'ac3'.
db.mydb.save({ "_id" : "ac3", "name" : "AC3 Phone", "brand" : "ACME", "type" : "phone", "price" : 900, "rating" : 5.6,"warranty_years" : 1, "available" : true })
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
db.mydb.find()
{ "_id" : "ac3", "name" : "AC3 Phone", "brand" : "ACME", "type" : "phone", "price" : 900, "rating" : 5.6, "warranty_years" : 1, "available" : true }
{ "_id" : "ac7", "name" : "AC7 Phone", "brand" : "ACME", "type" : "phone", "price" : 320, "rating" : 4, "warranty_years" : 1, "available" : false }
{ "_id" : "ac9", "name" : "AC9 Phone", "brand" : "ACME", "type" : "phone", "price" : 320, "rating" : 4, "warranty_years" : 1, "available" : false }
{ "_id" : "ac11", "name" : "AC11 Phone", "brand" : "ACME", "type" : "phone", "price" : 340, "rating" : 4, "warranty_years" : 1, "available" : false }
{ "_id" : "ac12", "name" : "AC12 Phone", "brand" : "ACME", "type" : "phone", "price" : 450, "rating" : 4.6, "warranty_years" : 2, "available" : true }
{ "_id" : "ac13", "name" : "AC13 Phone", "brand" : "ACME", "type" : "phone", "price" : 6000, "rating" : 5, "warranty_years" : 2.5, "available" : true }

MongoDB findOneAndUpdate() method

The findOneAndUpdate() method updates the values in the existing document.

Syntax

The basic syntax of findOneAndUpdate() method is as follows −

db.COLLECTION_NAME.findOneAndUpdate(SELECTIOIN_CRITERIA, UPDATED_DATA)

Example

Assume we have created a collection named stuDetail and inserted three documents in it as shown below −

db.stuDetails.insertMany([
{
"_id":0,
"name":"aimee Zank",
"class":10},
{
"_id":1,
"name":"Aurelia Menendez",
"class":11},
{
"_id":2,
"name":"Salena Olmos",
"class":12
}])
{ "acknowledged" : true, "insertedIds" : [ 0, 1, 2 ] }

Following example updates the class of the document with name 'Salena Olmos'
db.stuDetails.findOneAndUpdate({"name":"Salena Olmos"},{$set:{"class":10}})
{ "_id" : 2, "name" : "Salena Olmos", "class" : 12 }
.

## MongoDB updateOne() method
This methods updates a single document which matches the given filter.
### Syntax
The basic syntax of updateOne() method is as follows −

db.COLLECTION_NAME.updateOne(, )

### Example

db.stuDetails.updateOne({"name":"aimee Zank"},{$set:{"class":11}})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }

## MongoDB updateMany() method
The updateMany() method updates all the documents that matches the given filter.
### Syntax
The basic syntax of updateMany() method is as follows −

db.COLLECTION_NAME.update(, )

### Example

db.stuDetails.updateMany({"class":{$gt:10}},{$set:{"class":10}})
{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }

You can see the updated values if you retrieve the contents of the document using the find method as shown below −

db.stuDetails.find()
{ "_id" : 0, "name" : "aimee Zank", "class" : 10 }
{ "_id" : 1, "name" : "Aurelia Menendez", "class" : 10 }
{ "_id" : 2, "name" : "Salena Olmos", "class" : 10 }

MongoDB – Query Document (Prev Lesson)
(Next Lesson) MongoDB – Delete Document
', { 'anonymize_ip': true });