Easy Tutorial
❮ Mongodb Install Php Driver Mongodb Capped Collections ❯

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:

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:

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:

❮ Mongodb Install Php Driver Mongodb Capped Collections ❯