MongoDB array modifier update data Here we'll look at the array modifier. Arrays are very useful data structures that we often see and use: not only can they be referenced by a lasso, but they can also be used as a collection. An array modifier, as its name implies, is used to modify an array, not to modify integers or strings. Array modifiers are not many, just a few, but after mastering it, will bring us a very convenient operation. Below, let's look at:> db.user.findOne () { "_id": ObjectId ("4ffcb2ed65282ea95f7e3304"), "Age": 23,& nbsp "Favorite": { "1": "reading", "2": "Swimming",   ; "3": "Listening Music" }, "fname": "Jeff", "height": 166, &n Bsp "LName": "Jiang", "relationships": [ { "FName": "Deng", "lname": "Pan" }, &NBS P { "fname": "Qiang", "lname": "H E " }, { "fname": "Dongren", "lname": "Zeng" } ]} above is my personal information document that is still being perfected. Assuming I recently made a good friend, I want to add him to my interpersonal relationship "relationships" array. At this point, $push modifier comes in handy. The $push is that if the specified key already exists, it adds an element to the end of the existing array and creates a new array if it does not. Let's add a new friend in the following. > db.user.update ({"_id": ObjectId ("4ffcb2ed65282ea95f7e3304")},{$push: {"Relationships": {"fname": "Xiong", " LName ":" LAN "}}) > Db.user.findOne () { " _id ": ObjectId (" 4ffcb2ed65282ea95f7e3304 "), " Age " : 23, "favorite": { "1": "reading", "2": "Swimming", "3": "Listening Music" }, "fname": "Jeff", "height": 166, "lname": "Jiang", "relationships": [ { &N Bsp "fname": "Deng", "lname": "Pan" }, { "fname": "Qiang", &NBS P "lname": "he" }, { "FName": "Dongren", "lname": "Zeng" }, { "fname": "Xiong", "Lnam E ":" LAN " } ]} if there is a minus, then how to" minus "the operation of the array. There are two modifiers that can achieve the "minus" of the array, $pop and $pull. $pop and $pull are different, let's experiment separately. First is $pop> db.user.update ({"_id": ObjectId ("4ffcb2ed65282ea95f7e3304")},{$pop: {"Relationships": 1}) > Db.user.findOne () { "_id": ObjectId ("4ffcb2ed65282ea95f7e3304"), "age": 23, " Favorite ": { " 1 ":" reading ", " 2 ":" Swimming ", "3": "Listening Music" &nbSp }, "fname": "Jeff", "height": 166, "lname": "Jiang", "Relati Onships ": [ { " fname ":" Deng ", & nbsp "lname": "Pan" }, { "fname": "Qiang", "lname": "he" }, &NB Sp { "fname": "Dongren", "Ln Ame ":" Zeng " } ]} as you can see from above, it removes the friend we just added, meaning it is removed from the last array. So, what if we want to remove it from the beginning of the array? It is simple to change the "1" above to "1" and it will be reversed. Here's a look at:> db.user.update ({"_id": ObjectId ("4ffcb2ed65282ea95f7e3304")},{$pop: {"Relationships": -1}}) > Db.user.findOne () { "_id": ObjectId ("4ffcb2ed65282ea95f7e3304"), "age": 23, " Favorite ": { "1": "reading", "2": "Swimming", "3 : "Listening music" }, "fname": "Jeff", "height": 166, "lname": " Jiang ", " relationships ": [ { " fname ":" Qiang ", " lname ":" he " }, { "fname": "Dongren", "lname": "Zeng" &N Bsp } ]} from the results we can see that it achieves our intended purpose. So what if we want to delete the middle of the array? At this point, $pull comes in handy. First, we put new friends in, and here I no longer demonstrate. But we can think of "dongren" this person is in the middle of the array. Now we're going to remove him:> db.user.update ({"_id": ObjectId ("4ffcb2ed65282ea95f7e3304")},{$pull: {"Relationships": {"fname": " Dongren "," lname ":" Zeng "}}) > Db.user.findOne () { " _id ": ObjectId (" 4ffcb2ed65282ea95f7e3304 "), "Age": 23,&nbsP "Favorite": { "1": "reading", "2": "Swimming",   ; "3": "Listening Music" }, "fname": "Jeff", "height": 166, &n Bsp "LName": "Jiang", "relationships": [ { "FName": "Qiang", "lname": "he" }, &NBS P { "fname": "Xiong", "lname": "L An " } ]} as you can see above, $pull can delete the data in the middle of the array. One more thing to note here is that $pull will delete all the matching data, and I won't do the experiment here. Next, let's look at what else $push is, and we'll insert the same data into the array to see how it:> db.user.update ({"_id": ObjectId ("4ffcb2ed65282ea95f7e3304")},{$ push:{"Relationships": {"fname": "Xiong", "lname": "LAN"}}) > Db.user.findOne () { "_id": ObjectId (" 4ffcb2ed65282ea95f7e3304 "), "Age": 23, "favorite": { "1": "reading", "2": "Swim Ming ", " 3 ":" Listening Music " }, " fname ":" Jeff ", " Hei Ght ": 166, " lname ":" Jiang ", " relationships ": [ { &NB Sp "fname": "Qiang", "lname": "he" }, { "fname": "Xiong", &N Bsp "lname": "LAN" }, { "FName": "Xiong", "lname": "LAN" } ]} knot The result is that it can be inserted into the array normally. And in the actual production environment, we do not want to see such a result, then, there are two more available modifiers: $ne and $addtoset. $ne The main judgment, if there is this value in the array, it is not inserted; > db.user.update ({"RelationshipS.fname ": {$ne:" Xiong "}},{$set: {" fname ":" Xiong "," lname ":" LAN "}}) > Db.user.findOne () { " _id ": ObjectId ("4ffcb2ed65282ea95f7e3304"), "age": 23, "favorite": { "1" : "Reading", "2": "Swimming", "3": "Listening music" }, "fname": "Jeff", "height": 166, "lname": "Jiang", "relationships ": [ { " fname ":" Qiang ", "lname": "he" }, { "fname": "Xiong", "lname": "LAN" }, &NB Sp { "fname": "Xiong", "Lnam E ": LAN" &NBSp } ]} It can be seen from the result that the data is no longer inserted because it already exists in the array. In fact, $addToSet better than $ne, it can judge the existence of the data, and it is used in combination with $each, but also in the array to insert multiple data, which is $ne can not do, the following we look at the use of $addtoset, here by the way combined $ Each uses:> db.user.update ({"_id": ObjectId ("4ffcb2ed65282ea95f7e3304")},{$addToSet: {"Relationships": {$each: [{" FName ":" Xiong "," lname ":" LAN "},{" fname ":" Dongren "," lname ":" Zeng "}]}}) > Db.user.findOne () { " _id ": ObjectId ("4ffcb2ed65282ea95f7e3304"), "age": 23, "favorite": { "1" : "Reading", "2": "Swimming", "3": "Listening music" }, "fname": "Jeff", "height": 166, "lname": "Jiang", "relationships ": [ { " fname ":" Qiang ", "lname": "he" }, { "FName ":" Xiong ", " lname ":" LAN " }, &NBSP ; { "fname": "Xiong", "lname": "L An " }, { " fname ":" Dongren ", " lname ":" Zeng " } ]} in the Modify statement, we want to insert the {" F " Name ":" Xiong "," lname ":" LAN "},{" fname ":" Dongren "," lname ":" Zeng "} two data, but in the original array, {" FName ":" Xiong "," lname ":" LAN "} This data already exists, so it only inserts the back one. Achieve the purpose we want. So, I personally prefer to use $addtoset. Sometimes arrays have multiple values, and we want to manipulate only a subset of them. If we copy the whole document, it would be too much trouble and foolish. Fortunately, MongoDB provides us with two easy ways to do this: by location or by operator "$". Let's take a look at the two ways to use them separately. The first is to manipulate the array position. Arrays are all beginning with 0, and you can select the elements directly as keys. For example, we want to add an age key pair to the first data of an array, and we can do this:> db.user.update ({"_id": ObjectId ("4ffcb2ed65282ea95f7e3304")},{$set: {" Relationships.0.age ":"}) > Db.user.findOne () { "_id": ObjectId ("4FFCB2ed65282ea95f7e3304 "), " age ": 23, " favorite ": { " 1 ":" Reading ", &NB Sp "2": "Swimming", "3": "Listening Music" }, "FN Ame ": Jeff", "height": 166, "lname": "Jiang", "relationships": [ { "age": 22, "fname": "Q Iang ", " lname ":" he " }, {& nbsp "fname": "Deng", "lname": "Pan" }, { "fname": "Xiong", &N Bsp "lname": "LAN" } ]} But in many cases we do not know the subscript of the elements of the array to be modified without querying the document in advance. Then the positioning operator "$" is very useful. Itis to locate and update the elements that the query document has matched. Let's see how it's used:> db.user.update ({"Relationships.fname": "Xiong"},{$set: {"Relationships.$.age": $}) > Db.user.findOne () { "_id": ObjectId ("4ffcb2ed65282ea95f7e3304"), "age": 23, " Favorite ": { " 1 ":" reading ", " 2 ":" Swimming ", "3": "Listening Music" }, "fname": "Jeff", "height": 166, " LName ":" Jiang ", " relationships ": [ { " Age ": 22, " fname ":" Qiang ", " lname ": "He" }, { "Age": 22, "fname": "Deng", "lname": "Pan"   ; }, &NBsp { "age": 22, "fname": "X Iong ", " lname ":" LAN " } ]}
MongoDB Array modifier update data (GO)