MongoDB mongosh
Update
Update Document
To update an existing document we can use the
updateOne()
or updateMany()
methods.
The first parameter is a query object to define which document or documents should be updated.
The second parameter is an object defining the updated data.
updateOne()
The updateOne()
method will update the first document that is found matching the provided query.
Let's see what the "like" count for the post with the title of "Post Title 1":
Now let's update the "likes" on this post to 2. To do this, we need to use the
$set
operator.
Check the document again and you'll see that the "like" have been updated.
Insert if not found
If you would like to insert the document if it is not found, you can use the upsert
option.
Example
Update the document, but if not found insert it:
db.posts.updateOne(
{ title: "Post Title 5" },
{
$set:
{
title: "Post Title 5",
body: "Body of post.",
category: "Event",
likes: 5,
tags: ["news", "events"],
date: Date()
}
},
{ upsert: true }
)
Try it Yourself »
updateMany()
The updateMany()
method will update all documents that match the provided query.
Example
Update likes
on all documents by 1. For this we will use the $inc
(increment) operator:
db.posts.updateMany({}, { $inc: { likes: 1 } })
Try it Yourself »
Now check the likes in all of the documents and you will see that they have all been incremented by 1.