Update Documents
mongo --host 127.0.0.1:27017
The examples on this page use the inventory collection. To create and/or populate the inventory collection, run the following:
db.inventory.deleteMany({})
{ "acknowledged" : true, "deletedCount" : 5 }
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" }
] );
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("5b56ce42e68eb0362ce719b2"),
ObjectId("5b56ce42e68eb0362ce719b3"),
ObjectId("5b56ce42e68eb0362ce719b4"),
ObjectId("5b56ce42e68eb0362ce719b5"),
ObjectId("5b56ce42e68eb0362ce719b6"),
ObjectId("5b56ce42e68eb0362ce719b7"),
ObjectId("5b56ce42e68eb0362ce719b8"),
ObjectId("5b56ce42e68eb0362ce719b9"),
ObjectId("5b56ce42e68eb0362ce719ba"),
ObjectId("5b56ce42e68eb0362ce719bb")
]
}
Update Documents in a Collection
To update a document, MongoDB provides update operators, such as $set, to modify field values.
Update Methods
Update a Single Document
The following example uses the db.collection.updateOne() method on the inventory collection to update the first document where item equals "paper":
db.inventory.updateOne(
{ item: "paper" },
{
$set: { "size.uom": "cm", status: "P" },
$currentDate: { lastModified: true }
}
)
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
The update operation:
- uses the
$setoperator to update the value of the size.uom field to "cm" and the value of the status field to "P", - uses the
$currentDateoperator to update the value of the lastModified field to the current date. If lastModified field does not exist,$currentDatewill create the field.
Update Multiple documents
The following example uses the db.collection.updateMany() method on the inventory collection to update all documents where qty is less than 50:
db.inventory.updateMany(
{ "qty": { $lt: 50 } },
{
$set: { "size.uom": "in", status: "P" },
$currentDate: { lastModified: true }
}
)
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
Replace a Document
To replace the entire content of a document except for the _id field, pass an entirely new document as the second argument to db.collection.replaceOne().
When replacing a document, the replacement document must consist of only field/value pairs; i.e. do not include update operators expressions.
_id field is immutable; however, if you do include the _id field, it must have the same value as the current value.
The following example replaces the first document from the inventory collection where item: "paper":
db.inventory.replaceOne(
{ item: "paper" },
{ item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 40 } ] }
)
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
db.inventory.find( {} )
{ "_id" : ObjectId("5b56ce42e68eb0362ce719b2"), "item" : "canvas", "qty" : 100, "size" : { "h" : 28, "w" : 35.5, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("5b56ce42e68eb0362ce719b3"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "in" }, "status" : "P", "lastModified" : ISODate("2018-07-24T07:02:47.805Z") }
{ "_id" : ObjectId("5b56ce42e68eb0362ce719b4"), "item" : "mat", "qty" : 85, "size" : { "h" : 27.9, "w" : 35.5, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("5b56ce42e68eb0362ce719b5"), "item" : "mousepad", "qty" : 25, "size" : { "h" : 19, "w" : 22.85, "uom" : "in" }, "status" : "P", "lastModified" : ISODate("2018-07-24T07:02:47.806Z") }
{ "_id" : ObjectId("5b56ce42e68eb0362ce719b6"), "item" : "notebook", "qty" : 50, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "P" }
{ "_id" : ObjectId("5b56ce42e68eb0362ce719b7"), "item" : "paper", "instock" : [ { "warehouse" : "A", "qty" : 60 }, { "warehouse" : "B", "qty" : 40 } ] }
{ "_id" : ObjectId("5b56ce42e68eb0362ce719b8"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }
{ "_id" : ObjectId("5b56ce42e68eb0362ce719b9"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "in" }, "status" : "P", "lastModified" : ISODate("2018-07-24T07:02:47.806Z") }
{ "_id" : ObjectId("5b56ce42e68eb0362ce719ba"), "item" : "sketchbook", "qty" : 80, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }
{ "_id" : ObjectId("5b56ce42e68eb0362ce719bb"), "item" : "sketch pad", "qty" : 95, "size" : { "h" : 22.85, "w" : 30.5, "uom" : "cm" }, "status" : "A" }
Behavior
Atomicity
All write operations in MongoDB are atomic on the level of a single document. For more information on MongoDB and atomicity, see Atomicity and Transactions.
_id Field
Once set, you cannot update the value of the _id field nor can you replace an existing document with a replacement document that has a different _id field value.
Document Size
When performing update operations that increase the document size beyond the allocated space for that document, the update operation relocates the document on disk.
Field Order
MongoDB preserves the order of the document fields following write operations except for the following cases:
- The
_idfield is always the first field in the document. - Updates that include renaming of field names may result in the reordering of fields in the document.
Upsert Option
If updateOne(), updateMany(), or replaceOne() includes upsert : true and no documents match the specified filter, then the operation creates a new document and inserts it. If there are matching documents, then the operation modifies or replaces the matching document or documents.
Write Acknowledgement
With write concerns, you can specify the level of acknowledgement requested from MongoDB for write operations. For details, see Write Concern.