MongoDB update the document update

MongoDB document update

MongoDB using  update ()  and  save ()  method to update the collection of documents. Let's take a detailed look at the application and the difference between the two functions.


update () method

update () method is used to update the existing document. Syntax is as follows:

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

Parameter Description:

  • Query  : query update, similar to the back where within sql update query.
  • Update  : Update objects and some of the newer operators (e.g., $, $ inc is ...) and the like, may be understood as a set behind the sql update query
  • upsert  : Optional, this parameter mean, if there is no record update, whether to insert objNew, true inserted, the default is false, not inserted.
  • Multi  : Optional, mongodb default is false, only updating the first record found, if this parameter is true, according to the conditions put many records to check out all the updates.
  • writeConcern  : Optional, throw-level exception.

Examples

We insert the following data set in col:

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

Then we have to update the title by the update () method (title):

>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" : "菜鸟教程",
        "url" : "http://www.runoob.com",
        "tags" : [
                "mongodb",
                "database",
                "NoSQL"
        ],
        "likes" : 100
}
>

We can see the title (title) from the original "MongoDB Tutorial" Update to "MongoDB."

The above statement will only modify a document found in the first, if you want to modify the same document and more, you need to set multi parameter to true.

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

save () method

save () method to replace the existing document by the incoming document. Syntax is as follows:

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

Parameter Description:

  • the Document  : document data.
  • writeConcern  : Optional, throw-level exception.

Examples

The following examples we replace the document data _id as 56064f89ade2f21f36b03136 of:

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

After the replacement is successful, we can () command to view the data by replacing the find

>db.col.find().pretty()
{
        "_id" : ObjectId("56064f89ade2f21f36b03136"),
        "title" : "MongoDB",
        "description" : "MongoDB 是一个 Nosql 数据库",
        "by" : "Runoob",
        "url" : "http://www.runoob.com",
        "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 );

Add only the first:

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

All added to it:

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 );

Guess you like

Origin blog.csdn.net/cxu123321/article/details/93307005