Updating MongoDB Document Data Using mongo shell

This chapter introduces the updating of MongoDB document data through the mongo shell. MongoDB supports different ways of updating implemented by various types of operators, and it also supports multiple update functions.

Prepare Test Data

Insert a batch of test data into the inventory collection.

db.inventory.insertMany( [
   { item: "canvas", qty: 100, size: { h: 28, w: 35.5, uom: "cm" }, status: "A" },
   { item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
   { item: "mat", qty: 85, size: { h: 27.9, w: 35.5, uom: "cm" }, status: "A" },
   { item: "mousepad", qty: 25, size: { h: 19, w: 22.85, uom: "cm" }, status: "P" },
   { item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "P" },
   { item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
   { item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
   { item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" },
   { item: "sketchbook", qty: 80, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
   { item: "sketch pad", qty: 95, size: { h: 22.85, w: 30.5, uom: "cm" }, status: "A" }
] );

Updating Operator Syntax

To update documents, MongoDB provides multiple update operators (e.g., $set) to modify field values.

The format of the update operator parameters is as follows:

{
  <update operator>: { <field1>: <value1>, ... },
  <update operator>: { <field2>: <value2>, ... },
  ...
}

Explanation:

  • : Update operator
  • : Field name
  • : Field parameter

$set Operator

Used to update documents and create a new field if the field does not exist.

Updating a Single Document

Update a document using the db.collection.updateOne() method.

db.inventory.updateOne(
   { item: "paper" },
   {
     $set: { "size.uom": "cm", status: "P" },
     $currentDate: { lastModified: true }
   }
)

Update explanation:

  • First, retrieve the first document using the query condition (item = paper).
  • Use the $set operator to update the value of the size.uom field to cm and update the value of the status field to P.
  • Use the $currentDate operator to update the value of the lastModified field to the current date. If the lastModified field does not exist, $currentDate will create the field.

Updating Multiple Documents

Use the db.collection.updateMany() method to update multiple documents that meet the condition.

db.inventory.updateMany(
   { "qty": { $lt: 50 } },
   {
     $set: { "size.uom": "in", status: "P" },
     $currentDate: { lastModified: true }
   }
)

Update explanation:

  • First, retrieve all documents using the query condition (qty < 50).
  • Use the $set operator to update the value of the size.uom field to in and update the value of the status field to P.
  • Use the $currentDate operator to update the value of the lastModified field to the current date. If the lastModified field does not exist, $currentDate will create the field.

Tip: The $lt operator represents the meaning of less than.

Replace Document

Use the db.collection.replaceOne() method to replace the content of a document (except for the _id field). The replacement document can have different fields from the original document. Since the _id field is immutable, it can be omitted. However, if you do include the _id field, the id value must be consistent with the id of the document being updated.

db.inventory.replaceOne(
   { item: "paper" },
   { item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 40 } ] }
)

Explanation:

  • First, use the query criteria (item=paper) to retrieve the first document.
  • Replace the matched document with the content of the second parameter of the replaceOne method.

Update Behavior

Atomicity

All write operations in MongoDB are atomic at the individual document level.

_id Field

The _id field is immutable and cannot be updated.