MongoDB Update Documentation

Source: Internet
Author: User
Tags documentation modifiers mongodb update numeric value

Let's start with a MongoDB program (PHP) Related issues:

In the development, I need to update the corresponding document content according to the _id of a document, when I use a string like this directly ("57584E289BEF19798BD1CAB4"), the direct array ("_id" = " 57584e289bef19798bd1cab4 "), found that the update failed, including conversion to object type is not possible, and finally know that you need to use MongoDB inside the method to convert:$id = New MongoId ($id), converted results

  

Only converted to this, can be identified by MongoDB, and then directly array ("_id" and $id) on it;

One, MongoDB update document (Speaker update ())

The MongoDB update document uses the update () method, but there is also a way to save (), although save () is the insertion method, but it has a mechanism that it scans all documents in the corresponding collection before executing the INSERT, to see if the inserted data exists in the collection, if there is , then call the MongoDB Update () method, update (overwrite), do not exist then insert;

The syntax for update in MONGODB:

    Db.collectionName.update ({condition},{requires modified field and value},{upsert/multiple})

  The update () operation of MongoDB focuses on modifiers:

 1. $inc : specifically used to increase or decrease numbers, only for integers, long integers, and double-precision floating-point numbers, the value of $inc key must be a number, cannot be an array, string, or other non-numeric value, if the key does not exist, it is created

    

$inc Add Direct Write number (reward:5), subtract (reward:-5), non-existent field directly created, note that must be a numeric type, cannot be an array or other non-numeric value such as a string

2. $set : used to specify the value to modify a field, the value of the other field is not changed, to avoid overwriting the entire document, the updated field does not exist to create

    

3. $unset: Delete a field (unset only knows the form of key:value, so the value behind it doesn't matter, as long as it sees the key will unset off)

    

4. Array modifiers

  

Continued: http://wangming1993.github.io/2015/12/15/mongo-modifier/

  

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.