MongoDB tutoria...login
MongoDB tutorial
author:php.cn  update time:2022-04-21 17:49:03

MongoDB update documentation


MongoDB uses the update() and save() methods to update documents in a collection. Next, let us look at the applications of the two functions and their differences in detail.


update() method

update() method is used to update an existing document. The syntax format is as follows:

db.collection.update(
   <query>,
   <update>,
   {
     upsert: <boolean>,
     multi: <boolean>,
     writeConcern: <document>
   }
)

Parameter description:

  • ##query : update query conditions, similar to sql update query behind where.

  • update : update object and some update operators (such as $, $inc...), etc., can also be understood as set within sql update query The following

  • upsert : Optional, this parameter means whether to insert objNew if there is no update record, true means insert, and the default is false. Not inserted.

  • #multi : Optional, mongodb default is false, only the first record found is updated. If this parameter is true, multiple conditions will be found. All records are updated.

  • writeConcern : Optional, the level at which the exception is thrown.

Example

We insert the following data into the collection col:

>db.col.insert({
    title: 'MongoDB 教程', 
    description: 'MongoDB 是一个 Nosql 数据库',
    by: 'php中文网',
    url: 'http://www.php.cn',
    tags: ['mongodb', 'database', 'NoSQL'],
    likes: 100
})

Then we update the title (title) through the update() method:

>db.col.update({'title':'MongoDB 教程'},{$set:{'title':'MongoDB'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })   # 输出信息
> db.col.find().pretty()
{
        "_id" : ObjectId("56064f89ade2f21f36b03136"),
        "title" : "MongoDB",
        "description" : "MongoDB 是一个 Nosql 数据库",
        "by" : "php中文网",
        "url" : "http://www.php.cn",
        "tags" : [
                "mongodb",
                "database",
                "NoSQL"
        ],
        "likes" : 100
}
>

You can see that the title has been updated from the original "MongoDB Tutorial" to "MongoDB".

The above statement will only modify the first discovered document. If you want to modify multiple identical documents, you need to set the multi parameter to true.

>db.col.update({'title':'MongoDB 教程'},{$set:{'title':'MongoDB'}},{multi:true})


save() method

save() method replaces an existing document with the incoming document. The syntax format is as follows:

db.collection.save(
   <document>,
   {
     writeConcern: <document>
   }
)

Parameter description:

  • ##document

    : document data.

  • writeConcern

    : Optional, the level at which the exception is thrown.

  • Example

In the following example, we replaced the document data with _id 56064f89ade2f21f36b03136:

>db.col.save({
	"_id" : ObjectId("56064f89ade2f21f36b03136"),
    "title" : "MongoDB",
    "description" : "MongoDB 是一个 Nosql 数据库",
    "by" : "php",
    "url" : "http://www.php.cn",
    "tags" : [
            "mongodb",
            "NoSQL"
    ],
    "likes" : 110
})

After the replacement is successful, we can use find() Command to view the replaced data

>db.col.find().pretty()
{
        "_id" : ObjectId("56064f89ade2f21f36b03136"),
        "title" : "MongoDB",
        "description" : "MongoDB 是一个 Nosql 数据库",
        "by" : "php",
        "url" : "http://www.php.cn",
        "tags" : [
                "mongodb",
                "NoSQL"
        ],
        "likes" : 110
}
>

More examples

Only update the first record:

db.col.update( { " count" : { $gt : 1 } } , { $set : { "test2" : "OK"} } );
All updates:

db. col.update( { "count" : { $gt : 3 } } , { $set : { "test2" : "OK"} },false,true );
Only add the first Bar:

db.col.update( { "count" : { $gt : 4 } } , { $set : { "test5" : "OK"} },true,false );
Add them all:

db.col.update( { "count" : { $gt : 5 } } , { $set : { "test5" : "OK"} },true,true );
All updates:

db.col.update( { "count" : { $gt : 15 } } , { $inc : { "count" : 1} },false,true );

Only update the first record:

db.col.update( { "count" : { $gt : 10 } } , { $inc : { "count" : 1} } ,false,false );