MongoDB Tutorial Lesson Eighth MongoDB Update documentation

Source: Internet
Author: User
Tags mongodb save mongodb tutorial mongodb update

The MongoDB update () and Save () methods are used to update the collection of documents. The update () method updates the existing document value, replacing the existing document with the Save () method in the file.

MongoDB Update () method

The update () method updates the existing document values.

Grammar:

The basic syntax for the update () method is as follows

>db.  Collection_name.  Update(selectioin_criteria, updated_data)         
Example

Consider the following data MyCol collection.

{ "_ID" : ObjectId(5983548781331adf45ec5), "Title":"MongoDB Overview"}{  "_id" : objectid ( 5983548781331adf45ec6  "title" : "NoSQL Overview" }{ "_id"  : objectid5983548781331adf45ec7  " Title ":" tutorials Point Overview "}     

The following example will set the new title ' MongoDB Overview ' file and update its title to "New MongoDB Tutorial"

>Db.MyCol.Update({' Title ':' MongoDB Overview '},{$set:{' Title ':' New MongoDB Tutorial '}})>Db.MyCol.Find(){ "_ID" : ObjectId(5983548781331adf45ec5), "Title":"New MongoDB Tutorial"}{  "_id"   :  objectid (5983548781331adf45ec6  "title" : "NoSQL Overview" Span class= "pun" >}{  "_id"  : objectid ( 5983548781331adf45ec7  "title" : "Yiibai Overview" }>     

MongoDB default will only update a single file to update multiple you need to set the parameter ' multi ' to True

>db.  MyCol.  Update({' title ':' MongoDB overview '},{$set: {' title ':' New MongoDB Tutorial '}},{multi:true})          
MongoDB Save () method

The Save () method replaces the existing document and the Save () method through the new document

Grammar

The basic syntax for the MongoDB Save () method is as follows:

>db.  Collection_name.  Save({_id:ObjectId(),new_data})         
Example

The following example will replace the file with _id as ' 5983548781331adf45ec7 '

>Db.MyCol.Save( { "_ID" : ObjectId(5983548781331adf45ec7), "Title":"Yiibai New Topic", "By":"Yiibai" })>Db.MyCol.Find(){ "_ID" : ObjectId(5983548781331adf45ec5), "Title":"Yiibai New Topic", "By":"Yiibai"}{  "_id"   :  objectid (5983548781331adf45ec6  "title" : "NoSQL Overview" Span class= "pun" >}{  "_id"  : objectid ( 5983548781331adf45ec7  "title" : "Yiibai Overview" }>     

MongoDB Tutorial Eighth Lesson MongoDB Update document

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.