3.14. MongoDB updates the document

发布时间 : 2025-10-25 13:34:24 UTC      

Page Views: 9 views

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 ); 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。