MongoDB Learning Notes (ii)

Source: Internet
Author: User
Tags modifier

MongoDB Basic Operation Update

Update method parameters

// query: Conditions for Updates // obj: Updated object // Boolean type, true: No new, false: Update only not new // Multi: Whether to allow bulk updates function (query, obj, Upsert, multi) {    //dosomething}
View Code

Document substitution:

var where={    "age": +}; var p1 = db.user.findOne (where);p 1.age+ +; // age Self-increment 1db.user.update (WHERE,P1); // Replace the first document that matches to a new document
View Code

Common modifiers:

  1. $set modifier: The $set modifier is used to modify the value of a field in a document, which is added if the field does not exist.
    var where={    "_id": ObjectId ("5634b21827480baffb199df4")}; var action={    "$set": {"hobby": "Run"}//$set modifier Updates or adds the specified field } Db.user.update (where,action); // Add a hoppy field for Deng Chao
    View Code

    Modify the type of a field

    var where={    "_id": ObjectId ("5634b21827480baffb199df4")}; var action={    "$set": {"hoppy": ["Run", "Sing", "Dance"]}// Change the previous hoppy field to an interest array }db.user.update (where,action);
    View Code

    Modify fields in an inline document

    varWhere={    "_id": ObjectId ("5634b21827480baffb199df4")};varaction={    "$set":    {        "Family":        {            "Wife": "Sun Li",            "Father": "Unknown",            "Monther": "Unknown",            "Sons": ["Deng Han", "Deng Han One"]}}} ;d b.user.update (where,action);//added Deng's super family relationship dataaction={    "$set": {"Family.father": "Deng"}//Change the Father field in the Deng Hyper Relationship document to Deng}db.user.update (where,action);
    View Code

    Delete a field from a document

    var where={    "_id": ObjectId ("5634b21827480baffb199df4")}; var action={    "$unset": {"Family.wife": 1}//$unset operator for removing a field }; Db.user.update (where,action);
    View Code
  2. $inc modifier: self-increment or decrement modifier, atomic operation.
     var  where={ "_id": ObjectId (" 5634b21827480baffb199df4 ")};  var  action={ "$inc": {"Age": 1}//  Age auto-increment 1,-1 is self-decrement, the operator can only be used for numeric types, otherwise it will be abnormal   
    view code
  3. $push Operator: Adds an element to the end of an array, and if the specified array does not exist, the array is created
     var  Where={ "_id": ObjectId ("5634b21827480baffb199df4"  var  action={ "$push": {"family.sons": " Deng Zi "}//  add an element to the end of the sons array, and if the sons array does not exist, it will be created   
    view code
  4. $each Operator: The $push operator with the $each operator enables you to add multiple elements to an array at once
     var  Where={ "_id": ObjectId ("5634b21827480baffb199df4"  var  action={ "$push": {"family.sons": {" $each ": [" Deng Zi qi "," Dengxiao "," Deng Jiu Mei "]}}//   
    view code
  5. $slice operator: Used to set the length of the array, when the value is positive, only the first n elements in the array are preserved, negative, the last n elements in the array are preserved
     var  Where={ "_id": ObjectId ("5634b21827480baffb199df4"  var  action={ "$push": {"family.sons": {" $each ": [" Deng Zi qi "," Deng Han One "," Deng Han "]," $slice ": -2}}//   
    view code
  6. $sort Operator: Sort elements in an array
     { "_id": ObjectId ("5634b21827480baffb199df4"  var  action={ "$push": {"family.sons": {" $each ": [" Deng Zi qi "," Deng Han One "," Deng Han "]," $sort ": -1}}//   ";d b.user.update (where,action);  
    view code
  7. $addToSet Operator: Adds a new element to the array and only adds
     var  where={ "_id": ObjectId ("5634b21827480baffb199df4"  var  action={ "$addToSet": {"family.sons ": {" $each ": [" Deng Zi qi "," Deng Han XI "]," $sort ": -1}}//   ";d B.user.update (where,action) only if the element is not in the array;  
    view code
  8. $pop Operator: Removes an element from the top or tail of an array, which can be used to simulate a queue or stack
     var  where= { "_id": ObjectId ("5634b21827480baffb199df4" ) }; var  action={ "$pop": {"family.sons":-1} //   
    view code
  9. $pull operator: Deletes all matching elements in the array
    var where={    "_id": ObjectId ("5634b21827480baffb199df4")}; var action={    "$pull": {"family.sons": "Deng Han One"}// Delete all specified elements in the specified array }; Db.user.update (where,action);d B.runcommand ("GetLastError");
    View Code
  10. To modify an element by the position of an array element:
  11. var where={    "_id": ObjectId ("5634b21827480baffb199df4")}; var action={    "$inc": {"Age": -5},// 5    "$set": {"family.sons.0": "Deng Zi qi" "},// Modify the value of the first element in the sons array directly by subscript: Deng Zi ;d b.user.update (where,action);
    View Code

    Use the locator:

    var where={    "family.sons.name": "Deng Zi qi"}; The var action={    //$ symbol is automatically positioned to match the array elements, such as the first match to the array element subscript 3, then $ can actually be considered to be 3    "$ Set ": {" Family.sons.$.name ":" Dengxiao "},};d b.user.update (where,action);
    View Code
  12. findandmodify function: Update and return a document
    db.user.findAndModify ({    // query condition    // Sort method    update: {$set: { Name: ' Zhang Jie '}, $inc: {age:2}},// Update, or use Remove:true to execute delete    new:true//  Returns the updated document });
    View Code

MongoDB Learning Notes (ii)

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.