Update Documents: Part 1
Learn and practice commands to perform updates on single and multiple documents.
Update single document
We use the below commands to update a single document.
db.<collection-name>.updateOne(
<filter query>,
<update document>,
<options>
);
Filter query
filter query
is used to build criteria to fetch documents. This should return only one document if used with the updateOne
method.
Update document
The updated document contains modifications that need to apply to the document. Through this document, we can:
- Replace a field value
- Add a new field
- Increment or decrement a number
- Add a new value in the array
- Remove a value from the array
- Perform modification on the document returned by
filter query
Options
Options allow us to be more specific about what elements we want to update on a given document. Below are some common examples.
Options.upsert
: This accepts onlyboolean
type values and the default value isfalse
. This is an optional parameter. When it’strue
and thefilter query
doesn’t find any record, it inserts a new document.writeConcern
: This was explained in the lesson Write Concern.collation
: This was explained in the lesson Collation and Hint.arrayFilters
: This allows us to specify an array of filter documents that determine which array elements are modified during an update. This concept is too advanced to cover in this course, so we’ll focus on the other elements of these options.
First, let’s insert some documents to perform update operations.
db.tasks.insertOne({name: "Learn MongoDB Topic 1",date: new Date(),priority: 1,status: "pending",user: "onkar",assignee: "onkar"});
The above code returns insertedId
, which is the primary key
of the document. This is what we’ll use to perform the update operations.
Example:
The below command updates the status
of the matching task with provided _id
.
db.tasks.updateOne({_id: ObjectId("60f7d855b307d94301b9cb90")}, {$set: {status: "completed"}});
The below output shows that the filter query
matched one document, (matchedCount: 1)
, and an update operation was applied to one document, (modifiedCount: 1)
.
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
We can use the below command to check stored documents.
db.tasks.find();
Update multiple documents
We use the below commands to update multiple documents.
db.<collection-name>.updateMany(
<filter query>,
<update document>,
<options>
);
The arguments filter query
, update document
, and options
behave the same as they do for updateOne
.
All the documents returned from the filter query
are updated by the update document
parameter. In the below example, the status
is updated on the documents returned by filter query
.
db.tasks.updateMany({_id: {$in: [ObjectId("60f7d855b307d94301b9cb90"),ObjectId("60f7d855b307d94301b9cb91")]}},{$set: {status: "completed"}});
The below output shows that the filter query
matched two documents, (matchedCount: 2)
, and it performed an update operation on two documents, (modifiedCount: 2)
.
{
acknowledged: true,
insertedId: null,
matchedCount: 2,
modifiedCount: 2,
upsertedCount: 0
}
We can also update documents using the bulkWrite
method.
Let’s insert some documents and practice the update operation in the terminal below.
What is the upsert
option used for when updating a document?