mongodb--modifying a document using modifiers

Source: Internet
Author: User
Tags delete key findone modifier modifiers mongodb

You can use modifiers to modify a document, such as adding or deleting a document's key value. Using the modifier first to navigate to a document and then add the appropriate modification options, you need to use the UPDATE statement

1. Modify the document $inc modifier

>Db.users.findOne ({'name':'CD'});                {"_id": ObjectId ("584eafa97629396db95535da"), "name": "CD", "Sex": "M", "information": { "Age": at, "Address": "Shanghai"}}>Db.users.Update({"Name": "CD"},... {"$inc": {"QQ":123456789}}); Writeresult ({"nmatched":1, "nupserted":0, "nmodified":1 })>Db.users.findOne ({'name':'CD'});                {"_id": ObjectId ("584eafa97629396db95535da"), "name": "CD", "Sex": "M", "information": { "Age": at, "Address": "Shanghai"}, "QQ":123456789}>

Command explanation:

Db.users.findOne ()---Query a document, you can add specific parameters, just return a document, such as the example above to query the document key name value for CD

Db.users.update ()-Update a document, typically with two parameters, one parameter to update the document, and another to modify the content

{"$inc": {"key": "Value"}}--> the value corresponding to key key, if key does not exist, create key

Process:

1. First query the document named CD to see the specific key values in the document

2. Updating documents in a collection using modifier $inc and UPDATE statements

3. Query the document named CD again, and compare the results of the query in step 1 to see if the updated document is successful

2. Modify the document $set modifier

>Db.users.findOne ({"Name": "SCD"}); {"_id": ObjectId ("58549695a14618fbeef3bf0f"), "name": "SCD", "Sex": "M", "Address": "Shanghai"} >Db.users.Update({"Name": "SCD"},... {"$Set": {" lang ":["Chinese", "中文版"]}}); Writeresult ({"nmatched":1, "nupserted":0, "nmodified":1 })>Db.users.findOne ({"Name": "SCD"});         {"_id": ObjectId ("58549695a14618fbeef3bf0f"), "name": "SCD", "Sex": "M", "Address": "Shanghai", "Lang":["Chinese", "中文版"]}>

3. $uset modifier, delete a key from the document

>Db.users.findOne ({"Name": "SCD"});         {"_id": ObjectId ("58549885a14618fbeef3bf11"), "name": "SCD", "Sex": "M", "Address": "Shanghai", "Lang":["Chinese", "中文版"]}>Db.users.Update({"Name": "SCD"},... {"$unset": {"Sex": "M"}}); Writeresult ({"nmatched":1, "nupserted":0, "nmodified":1 })>Db.users.findOne ({"Name": "SCD"}); {"_id": ObjectId ("58549885a14618fbeef3bf11"), "name": "SCD", "Address": "Shanghai", "Lang": ["Chinese", "中文版"]}>

Operation Result: Delete key Name value for SCD document key sex

4. Differences between $set and $inc modifiers

$inc can only be modified for integer, long, or double-precision floating-point values

>Db.users.findOne ({"Name": "SCD"}); {"_id": ObjectId ("58549885a14618fbeef3bf11"), "name": "SCD", "Address": "Shanghai", "Lang": ["Chinese", "中文版"]}>Db.users.Update({"Name": "SCD"},... {"$inc": {"Address": "Beijing"}); Writeresult ({"nmatched":0, "nupserted":0, "nmodified":0, "Writeerror": {"code": -, "errmsg": "Cannot increment withNon-numeric argument: {address: \ "Åœ 椾 with \"} "}})

Use the $inc modifier key to the value of the string will be error "Writeerror"

Using $set, you can modify the value of the key corresponding to the character type.

> Db.users.findOne ({"Name": "SCD"}); {        "_id": ObjectId ("58549885a14618fbeef3bf11"),        "name": "SCD",        "address": "Shanghai",        [                 "Chinese",                "中文版"        ]}
>Db.users.Update({"Name": "SCD"},... {"$Set": {" Address ":" Beijing "}}); Writeresult ({"nmatched":1, "nupserted":0, "nmodified":1 })>db.users.findOne ("name": "SCD"); .- A-17t09: -:20.213+0800E QUERY[Thread1]syntaxerror:missing) after argument list @ (Shell):1: at>Db.users.findOne ({"Name": "SCD"}); {"_id": ObjectId ("58549885a14618fbeef3bf11"), "name": "SCD", "Address": "Beijing", "Lang": ["Chinese", "中文版"]}>

$set modifiers can modify many types of numeric values, such as strings, arrays, and so on

mongodb--modifying a document using modifiers

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.