Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

MongoDB modify data update and save method

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Earlier, I briefly introduced how MongoDB inserts data. Then the data is inserted into the database and the data may be modified. In MongoDB, it provides a way to modify data: update. Next, let me briefly talk about how to use update.

First of all, the data we operate is stored in the name collection in the test database. There are four records in the collection as follows:

> db

Test

> show collections

Name

System.indexes

> db.name.find ()

{"_ id": ObjectId ("5059221f955cfb1fd75066cb"), "fname": "jeff", "lname": "jiang"}

{"_ id": ObjectId ("5059223a955cfb1fd75066cc"), "fname": "xiaoqiang", "lname": "he"}

{"_ id": ObjectId ("50592245955cfb1fd75066cd"), "fname": "dengdeng", "lname": "pan"}

{"_ id": ObjectId ("50592253955cfb1fd75066ce"), "fname": "guage", "lname": "zeng"}

Let's try to modify the data. In the previous insert, I said that you can insert as a document, or you can assign a document to a variable and then insert it as a variable. update method can also be used in both ways. I first modify the second record in the collection as a document, with the following command:

> db.name.update ({"_ id": ObjectId ("5059223a955cfb1fd75066cc")}, {"fname": "qiang", "lname": "he"})

> db.name.find () # check found that the data has been modified, as expected

{"_ id": ObjectId ("5059221f955cfb1fd75066cb"), "fname": "jeff", "lname": "jiang"}

{"_ id": ObjectId ("5059223a955cfb1fd75066cc"), "fname": "qiang", "lname": "he"}

{"_ id": ObjectId ("50592245955cfb1fd75066cd"), "fname": "dengdeng", "lname": "pan"}

{"_ id": ObjectId ("50592253955cfb1fd75066ce"), "fname": "guage", "lname": "zeng"}

We then use variables to modify the third record in the collection. First, find it and assign it to the variable someone:

> someone=db.name.findOne ({"_ id": ObjectId ("50592245955cfb1fd75066cd")})

{

"_ id": ObjectId ("50592245955cfb1fd75066cd")

"fname": "dengdeng"

"lname": "pan"

}

Re-assign what you want to modify to change the value of the variable someone:

> someone.fname= "deng"

Deng

Replace the location of the previous document with a modified variable for the purpose of modifying the data:

> db.name.update ({"_ id": ObjectId ("50592245955cfb1fd75066cd")}, someone)

> db.name.find () # check found that the data has been modified, as expected

{"_ id": ObjectId ("5059221f955cfb1fd75066cb"), "fname": "jeff", "lname": "jiang"}

{"_ id": ObjectId ("5059223a955cfb1fd75066cc"), "fname": "qiang", "lname": "he"}

{"_ id": ObjectId ("50592245955cfb1fd75066cd"), "fname": "deng", "lname": "pan"}

{"_ id": ObjectId ("50592253955cfb1fd75066ce"), "fname": "guage", "lname": "zeng"}

As I said before, the save function actually calls the insert or update function based on the parameter condition. So can the save method also modify the data? Let's experiment with it. First of all, modify the operation in the form of a document:

> db.name.save ({"_ id": ObjectId ("50592253955cfb1fd75066ce")}, {"fname": "dongren", "lname": "zeng"})

> db.name.find () # check shows that the data has been modified, but it is different from the expected result. Like inserting data before, the save method also "ignores" the second parameter.

{"_ id": ObjectId ("5059221f955cfb1fd75066cb"), "fname": "jeff", "lname": "jiang"}

{"_ id": ObjectId ("5059223a955cfb1fd75066cc"), "fname": "qiang", "lname": "he"}

{"_ id": ObjectId ("50592245955cfb1fd75066cd"), "fname": "deng", "lname": "pan"}

{"_ id": ObjectId ("50592253955cfb1fd75066ce")}

The correct mode of operation is as follows:

> db.name.save ({"_ id": ObjectId ("50592253955cfb1fd75066ce"), "fname": "dongren", "lname": "zeng"})

> db.name.find () # check found that the data has been modified, as expected

{"_ id": ObjectId ("5059221f955cfb1fd75066cb"), "fname": "jeff", "lname": "jiang"}

{"_ id": ObjectId ("5059223a955cfb1fd75066cc"), "fname": "qiang", "lname": "he"}

{"_ id": ObjectId ("50592245955cfb1fd75066cd"), "fname": "deng", "lname": "pan"}

{"_ id": ObjectId ("50592253955cfb1fd75066ce"), "fname": "dongren", "lname": "zeng"}

The following is modified in the form of a variable:

> someone=db.name.findOne ({"_ id": ObjectId ("50592253955cfb1fd75066ce")})

{

"_ id": ObjectId ("50592253955cfb1fd75066ce")

"fname": "dongren"

"lname": "zeng"

}

> someone.fname= "guage"

Guage

> db.name.save (someone)

> db.name.find () # check found that the data has been modified, as expected

{"_ id": ObjectId ("5059221f955cfb1fd75066cb"), "fname": "jeff", "lname": "jiang"}

{"_ id": ObjectId ("5059223a955cfb1fd75066cc"), "fname": "qiang", "lname": "he"}

{"_ id": ObjectId ("50592245955cfb1fd75066cd"), "fname": "deng", "lname": "pan"}

{"_ id": ObjectId ("50592253955cfb1fd75066ce"), "fname": "guage", "lname": "zeng"}

Next, we will also look at the specific implementation of save and update. The first is the save method:

> db.name.save

Function (obj) {

If (obj = = null | | typeof obj = = "undefined") {

Throw "can't save a null"

}

If (typeof obj = = "number" | | typeof obj = = "string") {

Throw "can't save a number or string"

}

If (typeof obj._id = = "undefined") {

Obj._id = new ObjectId

Return this.insert (obj)

} else {

Return this.update ({_ id:obj._id}, obj, true)

}

}

Parameter description:

Obj: the record to be updated can only be a single record. If collection does not have the same id as obj, add a record, otherwise update.

Let's take a look at the update method:

> db.name.update

Function (query, obj, upsert, multi) {

Assert (query, "need a query")

Assert (obj, "need an object")

Var firstKey = null

For (var k in obj) {

FirstKey = k

Break

}

If (firstKey! = null & & firstKey [0] = "$") {

This._validateObject (obj)

} else {

This._validateForStorage (obj)

}

If (typeof upsert = = "object") {

Assert (multi = undefined, "Fourth argument must be empty when specifying upsert and multi with an object.")

Opts = upsert

Multi = opts.multi

Upsert = opts.upsert

}

This._db._initExtraInfo ()

This._mongo.update (this._fullName, query, obj, upsert? True: false, multi? True: false)

This._db._getExtraInfo ("Updated")

}

Parameter description:

1) query: query condition, similar to the content after where in the update statement

2) the object of obj:update and some updated operators (such as $, $inc, etc.) can also be understood as the content after set in the update statement of a relational database.

3) upsert: if there is no record of update, whether to insert the new document of obj. True: insert. Default is false. Do not insert.

4) multi: the default is false, and only the first record found is updated. If it is true, all the records found according to the conditions are updated.

The update method must receive at least two parameters: the object to be modified (query) and the modified object (obj), and the default values for the latter two parameters are false. Let me briefly experiment with the basic usage of the last two parameters.

First of all, let's talk about the function of the fourth parameter multi: the default is false, and only the first record found is updated. If it is true, all the records found according to conditions are updated.

> db.name.update ({fname: "jeff"}, {$set: {lname: "li"}}, false,false) # in order to let you see clearly, I wrote out the last two values. You don't have to write them here, because the default is false. The modifier $set is used here, which I will introduce later.

> db.name.find () # the value of parameter multi is false, and only the first record found is updated

{"_ id": ObjectId ("5059221f955cfb1fd75066cb"), "fname": "jeff", "lname": "li"}

{"_ id": ObjectId ("5059223a955cfb1fd75066cc"), "fname": "qiang", "lname": "he"}

{"_ id": ObjectId ("50592245955cfb1fd75066cd"), "fname": "deng", "lname": "pan"}

{"_ id": ObjectId ("50592253955cfb1fd75066ce"), "fname": "guage", "lname": "zeng"}

{"_ id": ObjectId ("50592ead955cfb1fd75066cf"), "fname": "jeff", "lname": "chen"}

{"_ id": ObjectId ("50592ecb955cfb1fd75066d0"), "fname": "jeff", "lname": "zhao"}

{"_ id": ObjectId ("50592edb955cfb1fd75066d1"), "fname": "jeff", "lname": "qian"}

> db.name.update ({fname: "jeff"}, {$set: {lname: "jiang"}}, false,true)

> db.name.find () # the value of the parameter multi is true to update all records found

{"_ id": ObjectId ("5059221f955cfb1fd75066cb"), "fname": "jeff", "lname": "jiang"}

{"_ id": ObjectId ("5059223a955cfb1fd75066cc"), "fname": "qiang", "lname": "he"}

{"_ id": ObjectId ("50592245955cfb1fd75066cd"), "fname": "deng", "lname": "pan"}

{"_ id": ObjectId ("50592253955cfb1fd75066ce"), "fname": "guage", "lname": "zeng"}

{"_ id": ObjectId ("50592ead955cfb1fd75066cf"), "fname": "jeff", "lname": "jiang"}

{"_ id": ObjectId ("50592ecb955cfb1fd75066d0"), "fname": "jeff", "lname": "jiang"}

{"_ id": ObjectId ("50592edb955cfb1fd75066d1"), "fname": "jeff", "lname": "jiang"}

Let's take a look at the role of the third parameter, upsert: if there is no record of update, whether to insert the new document of obj. True is insert, default is false, do not insert.

> db.name.update ({fname: "jeffery"}, {$set: {lname: "jiang"}}, false,true)

> db.name.find () # the value of parameter upsert is false and is not inserted

{"_ id": ObjectId ("5059221f955cfb1fd75066cb"), "fname": "jeff", "lname": "jiang"}

{"_ id": ObjectId ("5059223a955cfb1fd75066cc"), "fname": "qiang", "lname": "he"}

{"_ id": ObjectId ("50592245955cfb1fd75066cd"), "fname": "deng", "lname": "pan"}

{"_ id": ObjectId ("50592253955cfb1fd75066ce"), "fname": "guage", "lname": "zeng"}

{"_ id": ObjectId ("50592ead955cfb1fd75066cf"), "fname": "jeff", "lname": "jiang"}

{"_ id": ObjectId ("50592ecb955cfb1fd75066d0"), "fname": "jeff", "lname": "jiang"}

{"_ id": ObjectId ("50592edb955cfb1fd75066d1"), "fname": "jeff", "lname": "jiang"}

> db.name.update ({fname: "jeffery"}, {$set: {lname: "jiang"}}, true,true)

> db.name.find () # the value of parameter upsert is true, insert. Learned another way to insert data!

{"_ id": ObjectId ("5059221f955cfb1fd75066cb"), "fname": "jeff", "lname": "jiang"}

{"_ id": ObjectId ("5059223a955cfb1fd75066cc"), "fname": "qiang", "lname": "he"}

{"_ id": ObjectId ("50592245955cfb1fd75066cd"), "fname": "deng", "lname": "pan"}

{"_ id": ObjectId ("50592253955cfb1fd75066ce"), "fname": "guage", "lname": "zeng"}

{"_ id": ObjectId ("50592ead955cfb1fd75066cf"), "fname": "jeff", "lname": "jiang"}

{"_ id": ObjectId ("50592ecb955cfb1fd75066d0"), "fname": "jeff", "lname": "jiang"}

{"_ id": ObjectId ("50592edb955cfb1fd75066d1"), "fname": "jeff", "lname": "jiang"}

{"_ id": ObjectId ("5059357ad3ba22406ad408e6"), "fname": "jeffery", "lname": "jiang"}

The operation of update method is actually a little complicated. Here I only briefly introduce its matrix operation. The complex operation is to make rational use of various modifiers to achieve more efficient and convenient management. About the use of modifiers, I will experiment in the future.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report