In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Today, I read the authoritative guide to mongodb. This book gives a detailed explanation of update. Because there are a lot of knowledge points, so record it on the blog. If I just read it, I think I will forget it tomorrow.
There are two ways to update a document:
1, document replacement 2, modifier replacement (just modify the contents of a single field)
You can take a look at the help documentation for the parameters of update.
Db > db.blog.updatefunction (query, obj, upsert, multi) query: condition obj: object, updated class capacity upsert: determine whether the updated condition exists multi: by default, the update only performs an operation on the first document that meets the matching criteria. If this is true, the content is configured, so all are updated.
Document replacement
The current data type is
Wangaimin > db.test.findOne ({"name": "bob"}) {"_ id": ObjectId ("58e4b6410b8bd344936c8553"), "name": "bob", "email": "bob@email.com.cn", "content": "nice post"}
I need to be like this:
Wangaimin > db.test.find ({"name": "bob"}) {"_ id": ObjectId ("58e4b6410b8bd344936c8553"), "name": "bob", "comments": {"email": "bob@email.com.cn", "content": "nice post"}}
The steps are as follows:
Wangaimin > var Test=db.test.findOne ({"name": "bob"}) Test.comments= {"email": Test.email, "content": Test.content} delete Test.contentdelete Test.emaildb.test.update ({"name": "bob"}, Test) actually there is a hole in this place, which is _ id. If you have multiple name= "bob" in the match, you will report an error. _ id must be unique, so delete Test._id can and can be executed. It's not written in detail here. You can think about it carefully. If you don't want to, you don't have a memory point.
Use modifiers:
1Participated set modifier
Before modification: wangaimin > db.test.find ({"name": "bob"}) {"_ id": ObjectId ("58e4b6410b8bd344936c8553"), "name": "bob", "comments": {"email": "bob@email.com.cn", "content": "nice post"} command: db.test.update ({"name": "bob"}) {$set: {"name": "BOB"}) modified: wangaimin > db.test.find ({"name": "BOB"}) {"_ id": ObjectId ("58e4b6410b8bd344936c8553"), "name": "BOB", "comments": {"email": "bob@email.com.cn" "content": "nice post"}} modify embedded document: command: wangaimin > db.test.update ({"name": "BOB"}, {$set: {"comments.content": "change le"}) after modification: wangaimin > db.test.find ({"name": "BOB"}) {"_ id": ObjectId ("58e4b6410b8bd344936c8553"), "name": "BOB" "comments": {"email": "bob@email.com.cn", "content": "change le"} 2 Note: the value of $inc key must be numeric, not a string, array or other non-numeric values db.test.find () {"_ id": ObjectId ("58e4bb4b0b8bd344936c8554"), "number": 10} wangaimin > db.test.update ({"number": 10}, {$inc: {"number": 3}) wangaimin > db.test.find () {"_ id": ObjectId ("58e4bb4b0b8bd344936c8554"), "number": 13}
3. Add an array by pushing
If the array already exists, $push adds an element to the end of the existing array, and if not, creates a new array
Db.test.find () {"_ id": ObjectId ("58e4bd5f0b8bd344936c8555"), "school": "bd"} db.test.update ({"school": "bd"}, {$push, {"list": {"name": "zhangsan", "age": 20}) db.test.find () {"_ id": ObjectId ("58e4bd5f0b8bd344936c8555"), "school": "bd", "list": [{"name": "zhangsan" "age": 20}]}
4, add multiple values using $each
Db.test.update ({"school": "bd"}, {$push: {"list": {$each: [{"name": "zhangsan", "age": 21}, {"name": "zhangsan", "age": 22}, {"name": "zhangsan", "age": 23}, {"name": "zhangsan", "age": 24}]}) {"_ id": ObjectId ("58e4bd5f0b8bd344936c8555"), "school": "bd" "list": [{"name": "zhangsan", "age": 20}, {"name": "zhangsan", "age": 21}, {"name": "zhangsan", "age": 22}, {"name": "zhangsan", "age": 23}, {"name": "zhangsan", "age": 24}]}
5. Use $slice to limit the number. You must use $each.
Use $slice to limit the length of the array. If $slice:-10, if the length of the array is less than 10 (after $push), then all elements will be retained. If the element of the array is greater than 10, only the last 10 elements will report an error. The value of the emphasis $slice must be negative.
Wangaimin > db.test.find ({"name": "test"}) {"_ id": ObjectId ("58e4c1d30b8bd344936c8556"), "name": "test", "id": [1 nMatched ": 1) WriteResult ({" nMatched ": 1) WriteResult "nUpserted": 0, "nModified": 1}) wangaimin > db.test.find ({"name": "test"}) {"_ id": ObjectId ("58e4c1d30b8bd344936c8556"), "name": "test", "id": [5,5,1,2,3]}
6. Use the array as a dataset ($ne,$addToSet)
Because the elements of the array are repeatable, setting this is so that the elements of the array cannot be repeated
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: 0
*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.