Full-Stack Web Development with Vue.js and Node

(singke) #1
Introducing MongoDB Chapter 3

updateOne()

This command updates only a single document in the collection. Here, we have inserted a
couple of user entries with the incorrect emails. For the user with the name Peter, the


email is [email protected]. Let's update this document using updateOne():


> db.users.updateOne(
{ "name": "Peter" },
{
$set: { "email": "[email protected]" }
}
)

This command will update Peter's email to [email protected]. The output is:


As the output says, the modifiedCount is 1 and the matchedCount is 1 , which means the


document with the given condition was found and updated.


updateMany()

This command is used to update multiple documents in a collection. The command for


updating documents with updateOne() and updateMany() is the same. To update the


multiple records, we specify the condition and then set the desired values:


> db.users.updateOne(
{ "name": "Peter" },
{
$set: { "email": "[email protected]" }
}
)
Free download pdf