MongoDB Chinese file in the database after the Update method updates the document, the Update method has two parameters, such as
Update (ARGS1,ARGS2)
ARGS1 refers to the condition of querying a document;
ARGS2 refers to what changes are made to the document being queried;
One, document replacement
> joe1= Db.post.findOne ({"Age": +}) {"_id": ObjectId ("54a530c3ff0df3732bac1680"), "id": 1, "name ":" Joe "," Age ": 20}> joe1.age=2121> db.post.update ({" id ": 1},joe1) Writeresult ({" nmatched ": 1," nupserted " : 0, "nmodified": 1}) >
The modified result:
> joe1= Db.post.findOne ({"Age": +}) {"_id": ObjectId ("54a530c3ff0df3732bac1680"), "id": 1, "name ":" Joe "," Age ": 21}>
Second, the use of modified device
1. $set modifier
$set used to specify the value of a field that is created if the field does not exist;
> Db.post.findOne () {"_id": ObjectId ("54a530c3ff0df3732bac167f"), "id": 0, "name" : "Joe", "Age": 65}
To increase the comments key:
> db.post.update ({"id": 0}, ... {$set: {"Comments": "I Love You"}}) Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1}) > Db.post.findOne () {"_id": Object ID ("54a530c3ff0df3732bac167f"), "id": 0, "name": "Joe", "Age": all, "comment S ":" I Love You "} >
To modify the value of the comments key:
> db.post.update ({"id": 0}, {$set: {"Comments": "I don ' t Love You"}}) Writeresult ({"nmatched": 1, "nupserted": 0, "NM Odified ": 1}" > Db.post.findOne () {"_id": ObjectId ("54a530c3ff0df3732bac167f"), "id": 0, "name": "Joe", "Age": All, "comments": "I don t Love You"} >
Modify the value of the comments array:
> db.post.update ({"id": 0}, {$set: {"Comments": ["i love you", "or", "I don" T love you "]}) writeresult ({ " nmatched " : 1, " nupserted " : 0, "Nmodified"  : 1 }) > db.post.findone ({"id": 0}) { "_id" : ObjectId ("54a530c3ff0df3732bac167f"), " ID " : 0, " name " : " Joe ", " "Age" : 65, "Comments" : [ "I love you ", " or ", "I don ' T love you" ] } >
To remove the value of comments:
> db.post.update ({"id": 0}, {$unset: {"Comments": 1}}) Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1}) > Db.post.findOne ({"id": 0}) {"_id": ObjectId ("54a530c3ff0df3732bac167f"), "id": 0, "Name": "Joe", "Age": $ >
2. Modify the inline document
> db.post.findone ({"id": 0}) { "_id" : objectid ("54a530c3ff0df3732bac167f"), "id" : 0, "name" : "Joe", "Age" : 65, " Comments " : { "1" : 1, "2" : 2, "3"  : 3 } } > Db.post.update ({"id": 0}, {$set: {"Comments.1": 4}) writeresult ({ "nmatched" : 1, "nupserted" : 0, "nmodified"  : 1 }) > db.post.findone ({"id": 0}) { "_id" : objectid ("54a530c3ff0df3732bac167f"), "id" : 0, "name" : "Joe", "Age" : 65, " Comments " : { "1" : 4, "2" : 2, "3" : 3 } } >
This article is from the "Margin with Wish" blog, please be sure to keep this source http://281816327.blog.51cto.com/907015/1598398
MongoDB Learning Note 8 Deep MongoDB update action: modifier $set