MongoDB array modifier Update data

Source: Internet
Author: User
Tags findone modifier modifiers

mongodb Array modifier update data
2013-04-22 10:20:40 I would like to say two sources: Jiang ZHIFAO's blog
collection I want to contribute
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,    "favorite": {        "1": "Reading",  & nbsp;      "2": "Swimming",        "3": "Listening Music "   },   " fname ":" Jeff ",   " height ": 166,   " LName ":" Jiang ",   " Relationships ": [        {             "fname": "Deng",             "lname": "Pan"        },         {  &NBsp;         "fname": "Qiang",             "lname": "he"        },         {            "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", & nbsp;       "2 ":" Swimming ",       " 3 ":" Listening Music "   },     "fname": "Jeff",    "height": 166,    "lname": "Jiang",    " Relationships ": [        {             "fname": "Deng",            "lname": " Pan "       },        {             "fname": "Qiang",             "lname": "he"        },         {            "fname": "Dongren",             "lname": "Zeng"        },        {             "fname": "Xiong",            "lname": " LAN "       }   "} If there is a plus minus, then how to "minus" the array of operations. 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"     },    "fname": "Jeff",    "height": 166,    "lname": "Jiang", & nbsp;   "Relationships": [        {            "fname": "Deng",             "lname": "Pan"        } ,        {            "FName": "Qiang",            "lname": "he"         },        {             "fname": "Dongren",             "lname": "Zeng"        }   ]} from the above can be seen, It removed the friend we just added, which means it was deleted from the end of the 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",  & nbsp;  "height": 166,    "lname": "Jiang",    "relationships": [         {            "fname": " Qiang ",           " lname ":" he "        },        {             "fname": "Dongren",            " LName ":" Zeng "       }   "} from the result it can be seen 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,    "favorite": {        "1": " Reading ",       " 2 ":" Swimming ",       " 3 ":" Listening Music "   },   " fname ":" Jeff ",   " height ": 166, & nbsp;  "lname": "Jiang",    "relationships": [        {             "fname": "Qiang",             "lname": "he"        },         {            "fname": "Xiong",             "lname": "LAN"        }    ]} 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": "Swimming",         "3": "Listening Music"    },    "fname": "Jeff",   & nbsp "Height": 166,    "lname": "Jiang",    "relationships": [        {            "fname": "Qiang",             "lname": "he"         },        {             "fname": "Xiong",            "lname": " Lan "       },        {             "fname": "Xiong",             "lname": "LAN"        }   ]} The results show 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"        },        {            "fname": "Xiong",             "lname": "LAN"         }   ]} 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"         },        {             "fname": "Xiong",            " LName ": LAN"        },        {             "fname": "Dongren",             "lname": "Zeng"        }    ]} In the modification statement, we would like to insert {"fname": "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 ",       " 2 ":" Swimming ",       " 3 ":" Listening Music "   },   " fname ":" Jeff ",   &nbsP "Height": 166,    "lname": "Jiang",    "relationships": [         {            "Age": 22,             "fname": "Qiang",             "lname": "he"        },         {            "fname": "Deng",             "lname": "Pan"         },        {             "fname": "Xiong",            "lname": "LAN"        }   ]} But in many cases, without querying the document in advance, we don't know the subscript to modify the elements of the array. The position operator "$" is thenIt is very useful. It is used to locate and update the elements of the query document that have been 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", & nbsp;   "Relationships": [        {             "Age": 22,             "fname": "Qiang",            "lname": "he"         },      &nbsP {            "Age": 22,             "fname": "Deng",             "lname": "Pan"        },         {            "Age": 22,             "fname": "Xiong",             "lname": "LAN"        }   ]}

MongoDB array modifier update data

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.