MongoDB Update Data

Source: Internet
Author: User
Tags mongodb update

Use the DB. Table name. Update () Updates data The specified table must be present with the following syntax: Db.collection.update (criteria, objnew, Upsert, Multi) criteria:update query criteria, 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.   For example we have a table that is nopk> Db.noPK.find () {"_id": ObjectId ("5a50642b908e6b07a84472a2"), "name": "JavaScript", "Value": " Vue.js "} {" _id ": ObjectId (" 5a50655b908e6b07a84472a3 ")," name ":" Python "," type ":" Script "}  we need to name the value of Python more New for the Shell to note is that MONGO is case-sensitive when you write Python incorrectly to Python is unable to update the data > db.noPK.update ({"name": "Python"}, {$set: {"name": "Shell"}} ) Writeresult ({"nmatched": 0, "nupserted": 0, "nmodified": 0}) > The returned result shows that the update entry is 0 because it was incorrectly written to python > Db.noPK.update ( {"Name": "Python"}, {$set: {"name": "Shell"}}) Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1}) > Db.nopk.f IND () {"_id": ObjectId ("5a50642b908e6b07a84472a2"), "name": "JAvascript "," Value ":" Vue.js "} {" _id ": ObjectId (" 5a50655b908e6b07a84472a3 ")," name ":" Shell "," type ":" Script "}>& NBSP;MONGODB Update is the default only update matches to the first data   if we want to update all the matches to the data, then multi to be set to true  for example: Our NOPK table has 3 name is shell> Db.noPK.find () {"_id": ObjectId ("5a50642b908e6b07a84472a2"), "name": "JavaScript", "Value": "Vue.js"} {"_id": objecti D ("5a50655b908e6b07a84472a3"), "name": "Shell", "type": "Script"} {"_id": ObjectId ("5a506b40908e6b07a84472a4"), "name" ":" Shell "," type ":" Script "} {" _id ": ObjectId (" 5a506b9d908e6b07a84472a5 ")," name ":" Shell "," type ":" Script "," Scri Pt_type ":" Bash_shell "}>  we want to update them all > db.noPK.update ({" Name ":" Shell "}, {$set: {" name ":" Xshell "}}, False, True) Writeresult ({"nmatched": 3, "nupserted": 0, "nmodified": 3})  

MongoDB 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.