MongoDB Updating Documents
MongoDB uses the update() and save() methods to update documents in a collection. Let's take a detailed look at the applications and differences between these two functions.
update() Method
The update() method is used to update existing documents. The syntax is as follows:
db.collection.update(
<query>,
<update>,
{
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>
}
)
Parameter Explanation:
- query: The query criteria for the update, similar to the WHERE clause in SQL update queries.
- update: The object to update and some update operators (such as $, $inc, etc.), also similar to the SET clause in SQL update queries.
- upsert: Optional. If this parameter is set, it means that if no record matching the update criteria exists, whether to insert a new object. True means to insert, and the default is false, meaning not to insert.
- multi: Optional. MongoDB defaults to false, which updates only the first found record. If this parameter is set to true, all records matching the criteria will be updated.
- writeConcern: Optional. The level at which exceptions are thrown.
Example
We insert the following data into the collection col:
>db.col.insert({
title: 'MongoDB Tutorial',
description: 'MongoDB is a Nosql database',
by: 'tutorialpro.org',
url: 'http://www.tutorialpro.org',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
})
Next, we update the title using the update() method:
>db.col.update({'title':'MongoDB Tutorial'},{$set:{'title':'MongoDB'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) # Output information
> db.col.find().pretty()
{
"_id" : ObjectId("56064f89ade2f21f36b03136"),
"title" : "MongoDB",
"description" : "MongoDB is a Nosql database",
"by" : "tutorialpro.org",
"url" : "http://www.tutorialpro.org",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 100
}
>
The title has been updated from "MongoDB Tutorial" to "MongoDB".
The above statement only modifies the first found document. If you want to modify multiple documents with the same criteria, you need to set the multi parameter to true.
>db.col.update({'title':'MongoDB Tutorial'},{$set:{'title':'MongoDB'}},{multi:true})
save() Method
The save() method replaces an existing document with the document passed in. If the _id primary key exists, it updates the document; if it does not exist, it inserts a new document. The syntax is as follows:
db.collection.save(
<document>,
{
writeConcern: <document>
}
)
Parameter Explanation:
- document: The document data.
- writeConcern: Optional. The level at which exceptions are thrown.
Example
In the following example, we replace the document with _id 56064f89ade2f21f36b03136:
>db.col.save({
"_id" : ObjectId("56064f89ade2f21f36b03136"),
"title" : "MongoDB",
"description" : "MongoDB is a Nosql database",
"by" : "tutorialpro",
"url" : "http://www.tutorialpro.org",
"tags" : [
>db.col.update({'title': 'MongoDB'}, {$set: {'title': 'MongoDB', 'description': 'MongoDB is a NoSQL database', 'by': 'tutorialpro', 'url': 'http://www.tutorialpro.org', 'tags': ['mongodb', 'NoSQL'], 'likes': 110}})
After successful replacement, we can view the updated data using the find() command:
>db.col.find().pretty()
{
"_id" : ObjectId("56064f89ade2f21f36b03136"),
"title" : "MongoDB",
"description" : "MongoDB is a NoSQL database",
"by" : "tutorialpro",
"url" : "http://www.tutorialpro.org",
"tags" : [
"mongodb",
"NoSQL"
],
"likes" : 110
}
>
More Examples
Update only the first record:
Update all records:
Add only the first one:
Add all of them:
Update all records:
Update only the first record: