3.14. MongoDB updates the document

发布时间 :2025-10-25 12:33:00 UTC      

MongoDB usage update() And save() Method to update the documents in the collection. Next, let’s take a detailed look at the application of the two functions and their differences.

3.14.1. 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>
   }
)

参数说明:

  • query The query condition of update is similar to that after where in sql update query.

  • update The object of update and some updated operators (such as $, $inc…) can also be understood as the following set in the sql update query

  • upsert : optional, this parameter means that if there is no record of update, whether to insert objNew,true is insert, default is false, do not insert.

  • multi : optional. Mongodb defaults to false. Only the first record found is updated. If this parameter is true, multiple records are checked out according to the condition and all of them are updated.

  • writeConcern Optionally, the level at which the exception is thrown

Example

We’re gathering. col Insert the following data into:

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

Then we pass through update() Method to update the title (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
}
>

You can see that the title (title) is updated from the original “MongoDB tutorial” to “MongoDB”.

The above statement will only modify the first found 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})

3.14.2. save() Method

save() The method replaces the existing document with the incoming document, and the_ id primary key is updated when it exists, and inserted if it does not exist. The syntax format is as follows:

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

参数说明:

  • document Document data

  • writeConcern Optionally, the level at which the exception is thrown

Example

In the following example, we replace the document data whose_ id is 56064f89ade2f21f36b03136:

>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 pass the find() Command to view the replaced data

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

3.14.3. More Instanc

Update only 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 item:

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

Add it all:

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

Update all:

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

Update only the first record:

db.col.update( { "count" : { $gt : 10 } } , { $inc : { "count" : 1}
},false,false );
Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.