Latest web development tutorials

MongoDB document is updated

MongoDB usingupdate () 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 an existing document. Syntax is as follows:

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

Parameter Description:

  • query: update query criteria, similar to the sql update query where the inner rear.
  • update: update objects and some of the newer operators (such as $, $ inc ...), etc., can also be understood as a set back within the sql update query
  • upsert: optional, meaning this argument is that if there is no record update, is inserted objNew, true to insert, the default is false, not inserted.
  • multi: Optional, mongodb default is false, only to find the first record to update, if this argument is true, according to the conditions put out many records check all the updates.
  • writeConcern: optional level exception is thrown.

Examples

We insert the following data collection col:

>db.col.insert({
    title: 'MongoDB 教程', 
    description: 'MongoDB 是一个 Nosql 数据库',
    by: '本教程',
    url: 'http://www.w3big.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.w3big.com",
        "tags" : [
                "mongodb",
                "database",
                "NoSQL"
        ],
        "likes" : 100
}
>

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

The above statement will modify the document found in the first, if you want to edit 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 by passing documents to replace the existing document. Syntax is as follows:

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

Parameter Description:

  • document: the document data.
  • writeConcern: optional level exception is thrown.

Examples

The following example we replaced the _id as 56064f89ade2f21f36b03136 document data:

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

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

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

More examples

Only update the first record:

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

Update all:

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

Add all added:

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

Update all:

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