In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
The update function seems to be a little more difficult to use than insert,remove and find, with a lot of arguments.
However, simple to write some less used skills, general updates are generally relatively simple.
About the $inc operation:
It is an atomic operation and can be incremented by integers, long integers or floating-point numbers, as follows:
> test= {... Name: Mike,... "friends": 32. "enemies": 2. "Balance": 155.6. } {"name": "Mike", "friends": 32, "enemies": 2, "Balance": 155.6} > db.updatetest.insert (test) WriteResult ({"nInserted": 1}) > db.updatetest.update ({"name": "Mike"}, {"$inc": {"Balance": 5.4}}) WriteResult ({"nMatched": 1, "nUpserted": 0) "nModified": 1}) > db.updatetest.find () {"_ id": ObjectId ("5625ab9b16a7b138cf6bf46f"), "name": "Mike", "friends": 32, "enemies": 2, "Balance": 161}
About the $set operation:
Specify a field value for the existing document, and create it if it does not exist, as follows:
Test= {"name": "Mike", "age": 32, "sex": "male", "location": "China"} > db.updatetest.insert (test) WriteResult ({"nInserted": 1}) > db.updatetest.find () {"_ id": ObjectId ("5625ae7616a7b138cf6bf471"), "name": "Mike", "age": 32, "sex": "male", "location": "China"}
Increase kinship:
> relatives= {"Wife": "May", "Father": "Joe", "Mother": "Jojo"} {"Wife": "May", "Father": "Joe", "Mother": "Jojo"} > db.updatetest.update ({"_ id": ObjectId ("5625ae7616a7b138cf6bf471"), {"$set": {"relatives": relatives}}) WriteResult ({"nMatched": 1, "nUpserted": 0) "nModified": 1}) > db.updatetest.find () {"_ id": ObjectId ("5625ae7616a7b138cf6bf471"), "name": "Mike", "age": 32, "sex": "male", "location": "China", "relatives": {"Wife": "May", "Father": "Joe", "Mother": "Jojo"}}
Manipulate arrays with update:
$push can add an element to the end of an array in an existing document, and if not, create a new array directly, as follows:
People= {"title": "mail list", "Phone": "010-123456"} > db.updatetest.insert (people) WriteResult ({"nInserted": 1}) db.updatetest.update ({"title": "mail list"}, {"$push": {"emails": "test@123.com"}) db.updatetest.update ({"title": "mail list"}) {"$push": {"emails": "test@123.com"}) > db.updatetest.find () {"_ id": ObjectId ("5625ae7616a7b138cf6bf471"), "name": "Mike", "age": 32, "sex": "male", "location": "China", "relatives": {"Wife": "May", "Father": "Joe" "Mother": "Jojo"} {"_ id": ObjectId ("5625b11216a7b138cf6bf472"), "title": "mail list", "Phone": "010-123456", "emails": ["test@123.com", "test@123.com"]}
$push allows repeatable insertion of records.
Push is also allowed to be used with slice,sort,each, as follows:
Db.updatetest.update ({"title": "mail list"}, {"$push": {"emails": {$each: ["test1@123.com", "test2@124.com", "test3@125.com"]}) > db.updatetest.find () {"_ id": ObjectId ("5625b2ba16a7b138cf6bf473"), "title": "mail list", "Phone": "010-123456", "emails": ["test@123.com" "test@123.com", "test1@123.com", "test2@124.com", "test3@125.com"]}
Used with $slice:
> db.updatetest.update ({"title": "mail list"}, {$push: {"top10": {"$each": ["KFC", "mcdonalds", "pizzahut"], "$slice":-3}) WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1})
When there is no top10 key, one is automatically created.
> db.updatetest.find () {"_ id": ObjectId ("5625b44d16a7b138cf6bf474"), "title": "mail list", "Phone": "010-123456", "emails": ["test1@123.com", "test2@124.com", "test3@125.com"], "top10": ["KFC", "mcdonalds", "pizzahut"]}
Now the top10 array happens to have three elements. If you insert two more, only the last three will be retained.
> db.updatetest.update ({"title": "mail list"}, {$push: {"top10": {"$each": ["starbucks", "illy"], "$slice":-3}) WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1}) > db.updatetest.find () {"_ id": ObjectId ("5625b44d16a7b138cf6bf474"), "title": "mail list", "Phone": "010-123456" "emails": ["test1@123.com", "test2@124.com", "test3@125.com"], "top10": ["pizzahut", "starbucks", "illy"]}
For $push, duplicate data can be inserted. If the email address already exists and you do not want to insert it again, you can use the $addToSet operation, as follows:
> db.updatetest.update ({"title": "mail list"},... {"$addToSet": {"emails": "test1@123.com"}...}) WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 0}) > db.updatetest.find () {"_ id": ObjectId ("5625b44d16a7b138cf6bf474"), "title": "mail list", "Phone": "010-123456" "emails": ["test1@123.com", "test2@124.com", "test3@125.com"], "top10": ["HP", "DELL", "MicroSoft"]} > db.updatetest.update ({"title": "mail list"}, {"$addToSet": {"emails": {$each: ["test1@123.com", "abc@321.com", "new@newaddress.com") "abc@321.com"]}) WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1}) > db.updatetest.find () {"_ id": ObjectId ("5625b44d16a7b138cf6bf474"), "title": "mail list", "Phone": "010-123456", "emails": ["test1@123.com", "test2@124.com", "test3@125.com" "abc@321.com", "new@newaddress.com"], "top10": ["HP", "DELL", "MicroSoft"]}
Location-based array modification:
An array of embedded documents, as follows:
> db.updatetest.insert ({
...
... "content": "Mongodb Update"
... "comments": [
... {
... "comment": "Good post"
... "author": "John"
... "votes": 0
...}
... {
... "comment": "too short"
... "author": "mike"
... "votes": 3
...}
... {
... "comment": "free watches"
... "author": "Alice"
... "votes":-1
...}
...]
...
.)
WriteResult ({"nInserted": 1})
You can use the $symbol as the locator to modify the key in comments. For example, modify the key comments.comment.author to change Alice to May as follows:
> db.updatetest.update ({"comments.author": "Alice"}, {"$set": {"comments.$.author": "May"}})
WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1})
> db.updatetest.find ()
{"_ id": ObjectId ("5625b44d16a7b138cf6bf474"), "title": "mail list", "Phone": "010-123456", "emails": ["test1@123.com", "test2@124.com", "test3@125.com", "abc@321.com", "new@newaddress.com"], "top10": ["HP", "DELL", "MicroSoft"]}
{"_ id": ObjectId ("5625bbef16a7b138cf6bf475"), "content": "Mongodb Update", "comments": [{"comment": "Good post", "author": "John", "votes": 0}, {"comment": "too short", "author": "mike", "votes": 3}, {"comment": "free watches", "author": "May" "votes":-1}]}
The last one is the findAndModify operation:
This operation can return the document before the update.
As follows:
> dc=db.runCommand ({"findAndModify": "updatetest"
... "query": {"title": "mail list"}
... "sort": {"_ id":-1}
... "update": {"$set": {"company": "QQ"}})
> dc.value
{
"_ id": ObjectId ("5625b44d16a7b138cf6bf474")
"title": "mail list"
"Phone": "10-123456"
"emails": [
"test1@123.com"
"test2@124.com"
"test3@125.com"
"abc@321.com"
"new@newaddress.com"
]
"top10": [
"HP"
"DELL"
"MicroSoft"
]
}
You can use remove for parameters about the location of the findAndModify operation at update, and delete the document if it matches the query.
There is also new, which indicates whether the document before the update or the document after the update is returned. The default is before the update.
Fields to be returned in the fields document (optional)
Upsert Boolean value. True indicates a upsert. Default is false.
Or you can do this:
> db.updatetest.findAndModify ({query: {"title": "mail list"}, update: {$set: {"company": "BBC"}}, new:true, sort: {"_ id":-1}, fields: {"emails": 1}, upsert:true})
{
"_ id": ObjectId ("5625b44d16a7b138cf6bf474")
"emails": [
"test1@123.com"
"test2@124.com"
"test3@125.com"
"abc@321.com"
"new@newaddress.com"
]
}
This statement modifies company to bbc, and then returns the field emails,new to indicate that updated data is returned.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 202
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.