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