We can update the existing documents by using the updateOne and updateMany methods as follows:
- Updating a single document:
We can update an existing document like this:
> db.<collection>.updateOne(<filter>, <update>, <options>)
Let's update the document where the name equals Sébastien in the <filter> parameter with the update data using the $set operator in the <update> parameter, as follows:
> db.user.updateOne(
{ name: "Sébastien" },
{
$set: { status: "ok" },
$currentDate: { lastModified: true }
}
)
You should get the following result:
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
The $set operator is used to replace the value of a field with the new value. It takes the following format:
{ $set: { <field1>: <value1>, ... } }
The $currentDate operator is used to set the value of a field to the current date. The...