## Update Specific Fields in a Document 

To change a field value, MongoDB provides update operators$^{12}$, such as $set to modify values. 

Some update operators, such as $set , will create the field if the field does not exist. See the individual update operator 13 reference. 

## Step 1: Use update operators to change field values. 

For the document with item equal to "MNO2" , use the $set operator to update the category field and the details field to the specified values and the $currentDate operator to update the field lastModified with the current date. 

db.inventory.update( { item: "MNO2" }, { $set: { category: "apparel", details: { model: "14Q3", manufacturer: "XYZ Company" } }, $currentDate: { lastModified: true } } ) 

The update operation returns a WriteResult object which contains the status of the operation. A successful update of the document returns the following object: 

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) 

The nMatched field specifies the number of existing documents matched for the update, and nModified specifies the number of existing documents modified. 

## Step 2: Update an embedded field. 

To update a field within an embedded document, use the dot notation . When using the dot notation, enclose the whole dotted field name in quotes. 

The following updates the model field within the embedded details document. 

db.inventory.update( { item: "ABC1" }, { $set: { "details.model": "14Q2" } } ) 

The update operation returns a WriteResult object which contains the status of the operation. A successful update of the document returns the following object: 

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) 

## Step 3: Update multiple documents. 

By default, the update() method updates a single document. To update multiple documents, use the multi option in the update() method. 

