MongoDB Update Documentation

Source: Internet
Author: User
Tags mongodb projection mongodb query mongodb update

Update () and Save () methods

Update () method updates the values in an existing document

Method: The original field

> DB. Collection_name.update (Selection_criteria, Updated_data)

Such as:

The document titled "MongoDB Overview" is set to "New Update MongoDB Overview".

First, the query operation:

> Db.mycol.find ({' title ': ' MongoDB overview '},{' _id ': 1, ' title ': 1})

{"_id": +, "title": "MongoDB Overview"}

Update action

> db.mycol.update ({' title ': ' MongoDB overview '},{$set:{' title ': ' New update ' MongoDB Overview '}})

Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})

> Query for updated results

> Db.mycol.find ({' _id ': 100},{' _id ': 1, ' title ': 1})

{"_id": +, "title": "New Update MongoDB Overview"}

MongoDB only updates one document by default, and if you update multiple documents, set multi to True

>db.mycol.update ({' title ': ' MongoDB Overview '}, {$set: {' title ': ' New update MongoDB overview '}},{multi:true})

Save () method

Grammar:

>db. Collection_name.save ({_id:objectid (), new_data})

MongoDB projection (select field)

MongoDB's Find () method, the second optional parameter that this method receives in the MongoDB query document is the list of fields to retrieve. In MongoDB, when you execute the Find () method, it displays all the fields of the document by default. To limit the fields that are displayed, you need to set the value for the field list to 1 or 0. 1 is used to display fields, and 0 for hidden fields.

Grammar:

>db. Collection_name.find ({},{key:1}) must have an empty curly brace {}, with multiple fields enclosed in a curly brace, separated by commas, shown as 1, hidden as 0

> db.mycollection.find ({},{"title": 1, "_id": 0})

MongoDB Update Documentation

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.