MongoDB Data Modification Summary

Source: Internet
Author: User
Tags bulk insert mongodb update

MongoDB Data Modification Summary
2013-10-26 10:08:30 I'd like to say two sources: Huhui's column
collection I want to contribute
MongoDB Data Modification Summary  1. Foreword recently in learning MongoDB, data modification This part of the content more, command more cumbersome, so will be some of the commonly used modification commands summarized in this blog, easy to learn in the future. 2. Command Summary 1). Insert () Db.collection.insert (x)   x is the object to be updated, only a single record, such as: [Plain]db.collection.insert ({_id:1,name: "Test", Count : 1})   When you need to bulk INSERT, you can use a for loop in the shell, such as: [Plain]for (var i=0;i<16;i++) {       Db.mytest.insert ({_id:i,name: "Test" +i,count:i})   }  This is the result of querying the inserted data with the Find () command:[plain]> Db.mytest.find ()  {"_id": 0, "name": "Test0", "Count": 0} {"_id": 1, "name": "Test1", "Count": 1} {"_id": 2, "Name": "Test2", "Count": 2} {"_id": 3, "name": "Test3", "Count": 3} {"_id": 4, "name": "Test4", "Count": 4} { "_id": 5, "name": "Test5", "Count": 5} {"_id": 6, "name": "Test6", "Count": 6} {"_id": 7, "name": "Test7", "Coun T ": 7} {" _id ": 8," name ":" Test8 "," Count ": 8} {" _id ": 9," name ":" Test9 "," Count ": 9} {" _id ":" Name ":" Te St10 "," Count ": Ten} {" _id ": One," name ":" Test11 "," Count ":} {" _id ":" Name " : "Test12", "Count":} {"_id": "Name": "test13", "Count":} {"_id": +, "name": "test14", "Count": 14} { "_id": "Name": "test15", "Count":} 2). Update () Db.collection.update (criteria, objnew, Upsert, Multi)     four parameters are described below: Criteria:update query conditions, Similar to the SQL update query within where the Objnew:update object and some updated operators (such as $, $inc ... ), and so on, can also be understood as Upsert after the set in SQL Update query: This parameter means that if there is no record of update, insert Objnew,true is inserted, default is False, not inserted. Multi:mongodb default is False, only update the first record found, if this parameter is true, the condition is checked out all the records are updated. Several query examples are as follows: Db.mytest.update ({count:{$gt: 1}},{$set: {name: ' OK '}})                                     update only the first record db.mytest.update ({count:{$gt: 3}},{$set: {name: "OK"}},false, True)                   All of the Db.mytest.update ({count:{$gt: 4}},{$set: {name: "Ok1" are updated with more than 3}},true,false)             updated only one db.mytest.update ({ count:{$gt: 6}},{$set: {name: "ok123"}},true,true)                more than 6 has all updated  3). Save () Db.collection.save (x) x is the object you want to insert, with the same effect as the Insert command above. The difference between save and insert is this: in the operation to insert the data, when the _id is encountered in the same situation, save completes the save operation, insert is saved, that is, _id in the same case, save is equivalent to the update operation.   Below are some of the MongoDB update operator  4). $inc usage: {$inc:{field:value}}   means to add value:[plain]> db.mytest.find ({_id:1}) to a Number field field        {"_id": 1, "name": "Test1", "Count": 1} > db.mytest.update ({_id:1},{$inc: {count:1}})  > db.mytest.find ({_id:1})  {"_id": 1, "name": "Test1", "Count": 2} //count field plus 1  value value Also can be negative, equivalent to minus one value:[plain]> db.mytest.update ({_id:1},{$inc: {count:-2}})  > Db.mytest.find ({_id:1})  {"_ ID ": 1," "Name": "Test1", "Count": 0} //value from 2 to 0  5). $set command usage: {$set: {Field:value}} is equivalent to closingThe set field=value of SQL in the system database, all data types support $set operation [plain]> Db.mytest.update ({_id:1},{$set: {count:111}})  > Db.mytest.find ({_id:1})  {"_id": 1, "name": "Test1", "Count": 111}  //Modify numeric type  > db.mytest.update ({_id:1},{$set: {name: "MongoDB"})  > Db.mytest.find ({_id:1})  {"_id": 1, "Count": 111, "name": "MongoDB"}   //Modify character type   6). $unset usage: {$unset:{field:1}}[plain]> db.mytest.find ({_id:1})  {"_id": 1, "Count": 111, "name": "MongoDB"}&nbsp ;> db.mytest.update ({_id:1},{$unset: {name:1}})  > Db.mytest.find ({_id:1})  {"_id": 1, "Count": 111}& nbsp deleted field name  7). $push usage: {$push: {field:value}} Append value To field, field must be data type, if field does not exist, an array type will be added to add:[plain]> Db.mytest.update ({_id:15},{$set: {array:["AAA", "BBB"]})  > Db.mytest.find ({_id:15})  {"_id": 15, " Array ": [ " AAA ", " BBB "]," Count ":" Name ":" ok123 "}  use push to append Data:[plain]> db.mytest.update ({_id: 15},{$push:{array: "CCC"}})  > db.mytest.find ({_id:15})  {"_id": "Array": [  "AAA",  "BBB",  "CCC"], "count": 15, ' Name ': ' ok123 '} push you can append only one value at a time, and if you need to append more than one value, you need to use $pushall:[plain]> db.mytest.update ({_id:15},{$pushAll: { array:["ddd", "Eee", "FFF"]})  > Db.mytest.find ({_id:15})  {"_id": "Array": [  "AAA",  "BBB" ,  "CCC",  "ddd",  "eee",  "FFF"], "count": +, "name": "Ok123"}  8). $addToSet usage: {$addToSet: {Field:value}} adds a value to the array and adds:[plain]> db.mytest.update only if the value is not within the array ({_id:15},{$ Addtoset:{array: "123"})  > Db.mytest.find ({_id:15})  {"_id": "Array": [  "AAA",  "BBB", & nbsp "123"], "array2": [  "MMM",  "nnn"], "count": +, "name": "Ok123"} > db.mytest.update ({_id:15},{$ad Dtoset:{array: "AAA"})  > Db.mytest.find ({_id:15})  {"_id": "Array": [  "AAA",  "BBB",   "123"], "array2": [  "MMM",  "nnn"], "count":"Name": "Ok123"}  9). $pop Delete one of the values in the array, delete the last value: {$pop: {field:1}}, delete the first value: {$pop:{field:-1}}[plain]> db.mytest.find ({_id:15})  {"_id" : "Array": [  "AAA",  "BBB",  "123"], "array2": [  "MMM",  "nnn"], "count": All, "name": "Ok123"} > db.mytest.update ({_id:15},{$pop: {array:1}})  > Db.mytest.find ({_id:15})  {"_id": 15 , "array": [  "AAA",  "BBB"], "array2": [  "MMM",  "nnn"], "count": +, "name": "Ok123"} & NBSP;10). $pull usage: $pull:{field:value}   Remove a value equal to value from the array:[plain]> db.mytest.find ({_id:15})  {"_id": 15, " Array ": [ " AAA ", " BBB "]," array2 ": [ " MMM ", " nnn "]," coun t ":," name ":" Ok123 "}&nbs P;> db.mytest.update ({_id:15},{$pull: {array: "AAA"}})  > Db.mytest.find ({_id:15})  {"_id":-"Array ": [ " BBB "]," array2 ": [ " MMM ", " nnn "]," Count ": 15, " name ":" Ok123 "}  11). $pullAll with$pull, you can delete multiple values in an array at once:[plain]> Db.mytest.find ({_id:15})  {"_id": "Array": [  "BBB"], "array2": [&nbsp ; "MMM",  "nnn"], "Count": "Name": "Ok123"} > db.mytest.update ({_id:15},{$pullAll: {array2:["MMM", "nnn "]})  > Db.mytest.find ({_id:15})  {" _id ":" Array ": [ " BBB "]," array2 ": []," count ": []," name ":" Ok123 "}  12). $ can be understood as an array locator, see an example of an official document:[plain]> T.find ()  {"_id": ObjectId ("4b97e62bf1d8c7152c9ccb74"), "title": "ABC", " Comments ": [{" By ":" Joe "," votes ": 3}, {" By ":" Jane "," votes ": 7}]} > t.update ({' comments.by ': ' Joe '}, {$inc: {' comments.$.votes ': 1}})  > t.find ()  {"_id": ObjectId ("4b97e62bf1d8c7152c9ccb74"), "title": "ABC", "comments": [{"By": "Joe", " Votes ": 4}, {" By ":" Jane "," votes ": 7}]}  It is important to note that $ will only find the first array item, followed by:[plain]> Db.mytest.find ({_id:16}) &nbsp ; {"_id": +, "x": [  1,  2,  3,  1]} > db.mytest.update ({x:1},{$inc: {"x.$": 1})  > Db.mytest.find ({_id:16})  {"_id": +, "x": [  2,  2,  3,  1]}&n bsp;  Another thing to note is that when used with $unset, a null array item is left, which can be resolved using {$pull: {x:null}} to resolve:[plain]> Db.mytest.find ({_id:16} )  {"_id": +, "x": [  2,  2,  3,  1]} > db.mytest.update ({x:3},{$unset: {"x.$": 1}}) &N Bsp;> db.mytest.find ({_id:16})  {"_id": +, "x": [  2,  2,  null,  1]} > db.mytest . Update ({_id:16},{$pull: {x:null}})  > Db.mytest.find ({_id:16})  {"_id": +, "x": [  2,  2,   1]}  

MongoDB Data Modification Summary

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.