In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
How to achieve document update operation in MongoDB, I believe that many inexperienced people are at a loss about this. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Document replacement
Suppose I now have the following piece of data in my collection:
{"_ id": ObjectId ("59f005402844ff254a1b68f6"), "name": "Romance of the three Kingdoms", "authorName": "Luo Guanzhong", "authorGender": "male", "authorAge": 99.0}
This is a book with a title and author information, but the author is a separate entity, so I want to extract it and look like this:
{"_ id": ObjectId ("59f005402844ff254a1b68f6"), "name": "Romance of the three Kingdoms", "author": {"name": "Luo Guanzhong", "gender": "male", "age": 99.0}}
I can do the following:
Another problem is that when updating, MongoDB will only match the first updated document, assuming that I have the following data in my MongoDB:
{"_ id": ObjectId ("59f00d4a2844ff254a1b68f7"), "x": 1} {"_ id": ObjectId ("59f00d4a2844ff254a1b68f8"), "x": 1} {"_ id": ObjectId ("59f00d4a2844ff254a1b68f9"), "x": 1} {"_ id": ObjectId ("59f00d4a2844ff254a1b68fa"), "x": 2}
I want to change all the data with x 1 to 99, and it's easy to think of the following command:
Db.sang_collect.update ({XRV 1}, {XRO 99})
But we found that the result of the implementation was as follows:
{"_ id": ObjectId ("59f00d4a2844ff254a1b68f7"), "x": 99} {"_ id": ObjectId ("59f00d4a2844ff254a1b68f8"), "x": 1} {"_ id": ObjectId ("59f00d4a2844ff254a1b68f9"), "x": 1} {"_ id": ObjectId ("59f00d4a2844ff254a1b68fa"), "x": 2}
That is, only the first match result has been updated, and nothing else has changed. This is the update rule of MongoDB, that is, only the first match is updated. If we want to update all x 1 to x 99, we can use the following command:
Db.sang_collect.update ({XRV 1}, {$set: {XRV 99}}, false,true)
First of all, the data we are going to modify is assigned to $set,$set as a modifier, which we will explain in detail below, and then there are two more parameters. The first false indicates whether to insert the document we want to update as a new document if there is no update record. True means to insert, false means not to insert, default is false, the second true indicates whether to update all found documents, and false indicates that only the first record will be updated True means to update all documents found.
Use modifier
Many times we modify the document only to modify some part of the article, not all of it, but now I am faced with such a problem, suppose I have the following document:
{x:1,y:2,z:3}
Now that I want to change the value of x in this document to 99, I might use the following:
Db.sang_collect.update ({XRV 1}, {XRO 99})
But the update turned out to be like this:
{"_ id": ObjectId ("59f02dce95769f660c09955b"), "x": 99}
As shown below:
MongoDB helped me update the whole document! To solve this problem, we can use modifiers.
$set modifier
$set can be used to modify the value of a field, and if it does not exist, create it. As follows:
If the field does not exist, create it as follows:
You can also delete a field using $unset, as follows:
$set can also be used to modify embedded documents, taking the previous book as an example, as follows:
{"_ id": ObjectId ("59f042cfcafd355da9486008"), "name": "Romance of the three Kingdoms", "author": {"name": "Luo Guanzhong", "gender": "male"}}
To change the author's name, do the following:
Db.sang_collect.update ({name: the Romance of the three Kingdoms}, {$set: {"author.name": "Luo Guanzhong in the Ming Dynasty"}})
The modification results are as follows:
{"_ id": ObjectId ("59f042cfcafd355da9486008"), "name": "Romance of the three Kingdoms", "author": {"name": "Luo Guanzhong in the Ming Dynasty", "gender": "male"}}
$inc modifier
$inc is used to increase the value of an existing key and create a new one if the key does not exist. For example, I would like to add an age of 99 to Luo Guanzhong above, as follows:
Db.sang_collect.update ({name: the Romance of the three Kingdoms}, {$inc: {"author.age": 99}})
The implementation results are as follows:
{"_ id": ObjectId ("59f042cfcafd355da9486008"), "name": "Romance of the three Kingdoms", "author": {"name": "Ming Dynasty Luo Guanzhong", "gender": "male", "age": 99.0}}
Join me to add one year to Luo Guanzhong and execute the following orders:
Db.sang_collect.update ({name: "Romance of the three Kingdoms"}, {$inc: {"author.age": 1}})
This adds 1 to the existing value, and the result is as follows:
{"_ id": ObjectId ("59f042cfcafd355da9486008"), "name": "Romance of the three Kingdoms", "author": {"name": "Ming Dynasty Luo Guanzhong", "gender": "male", "age": 100.0}}
Note that $inc can only be used to manipulate numbers, not null, Boolean, and so on.
Array modifier
There are several array modifiers, let's look at them separately.
$push can append elements to the end of an existing array. Create an array if it does not exist, or take our above book as an example. Suppose book has a field of comments, an array, indicating a comment on the book. We can add a comment using the following command:
Db.sang_collect.update ({name: the Romance of the three Kingdoms}, {$push: {comments: "good Book 666"}})
The comments field does not exist at this time, and the system will automatically create it for us. The result is as follows:
{"_ id": ObjectId ("59f042cfcafd355da9486008"), "name": "Romance of the three Kingdoms", "author": {"name": "Luo Guanzhong", "gender": "male", "age": 100.0}, "comments": ["good Book 666"]}
At this point, we can add comments as follows:
Db.sang_collect.update ({name: "Romance of the three Kingdoms"}, {$push: {comments: "good Book 666 la"}})
The results are as follows:
{"_ id": ObjectId ("59f042cfcafd355da9486008"), "name": "Romance of the three Kingdoms", "author": {"name": "Luo Guanzhong", "gender": "male", "age": 100.0}, "comments": ["good Book 666", "good Book 666 la"]}
If you want to add three comments at a time, you can use them with $each, as follows:
Db.sang_collect.update ({name: the Romance of the three Kingdoms}, {$push: {$each: {111,222,333]}})
The results are as follows:
{"_ id": ObjectId ("59f042cfcafd355da9486008"), "name": "Romance of the three Kingdoms", "author": {"name": "Luo Guanzhong" in the Ming Dynasty, "gender": "male", "age": 100.0}, "comments": ["good Book 666", "good Book 666 La", "111,222,333"]}
We can use $slice to fix the length of the array, assuming that the length of my fixed array is 5. If there are less than 5 elements in the array, all of them will be preserved. If there are more than 5 elements in the array, only the latest 5 will be retained, as follows:
Db.sang_collect.update ({name: the Romance of the three Kingdoms}, {$push: {$each: {$each: ["444", "555"], $slice:-5}})
Note: the value of $slice is negative. The running result is as follows:
{"_ id": ObjectId ("59f042cfcafd355da9486008"), "name": "Romance of the three Kingdoms", "author": {"name": "Luo Guanzhong", "gender": "male", "age": 100.0}, "comments": [111,222,333,444,555]}
We can also use $sort to sort the data before cleaning up, and then clean it up. For example, I have an class document like this:
{"_ id": ObjectId ("59f07f3649fc5c9c2412a662"), "class": "Class two, Grade three"}
Now insert student into this document, each student has a name and grade, and then sort it in descending order, as long as the first five items are as follows:
Db.sang_collect.update ({class: "Class two, Grade three"), {$push: {students: {$each: [{name: "Zhang 100", score:100}, {name: "Zhang Jiujiu", score:99}, {name: "Zhang 98", score:98}], $slice:5,$sort: {score:-1})
The values of $sort are-1 and 1 for descending order and 1 for ascending order.
After the above command has been executed twice (that is, inserted twice), the result is as follows:
{"_ id": ObjectId ("59f07f3649fc5c9c2412a662"), "class": "Class two, Grade three", "students": [{"name": "Zhang Bai", "score": 100.0}, {"name": "Zhang 100", "score": 100.0}, {"name": "Zhang Jiujiu", "score": 99.0}, {"name": "Zhang Jiujiu" "score": 99.0}, {"name": "Zhang 98", "score": 98.0}]}
$slice and $sort cannot be used only with $push, but also with $each.
$addToSet
We can use $addToSet when inserting, indicating that the value to be inserted will not be inserted if it exists, otherwise it will be inserted as follows:
Db.sang_collect.update ({name: the Romance of the three Kingdoms}, {$addToSet: {comments: good Book})
After executing the above command several times, it was found that only one piece of data had been successfully inserted. You can also combine $addToSet with $each, as follows:
Db.sang_collect.update ({name: the Romance of the three Kingdoms}, {$addToSet: {$each: {111,222,333]}})
$pop
$pop can be used to delete data from an array, as follows:
Db.sang_collect.update ({name: the Romance of the three Kingdoms}, {$pop: {comments:1}})
1 removes a piece of data from the end of the comments array, and-1 removes a piece of data from the beginning of the comments array.
$pull
With $pull, we can conditionally delete an element in the array, as follows:
Db.sang_collect.update ({name: "Romance of the three Kingdoms"}, {$pull: {comments: "444"}})
Represents the deletion of data with a value of 444 in the array.
$
Since it is an array, of course we can access it through the subscript. The following line of operation indicates that the (first comments) comments with a subscript of 0 is changed to 999:
Db.sang_collect.update ({name: "Romance of the three Kingdoms"}, {$set: {"comments.0": "999"}})
But sometimes I don't know where the data I want to modify is in the array, so I can use the $symbol to solve it:
Db.sang_collect.update ({comments: "333"}, {$set: {"comments.$": "333-1"}})
The query condition finds the subscript of 333, and the $symbol is the subscript of the code, and then it can be modified by the $symbol.
Save
Save is a function in shell that receives a parameter, which is the document. If there is a _ id parameter in the document, save will perform the update operation, otherwise the insert operation will be performed. Using the save operation, we can easily complete some update operations.
A command similar to the following indicates an insert operation (because there is no _ id):
Db.sang_collect.save ({XRV 111}) after reading the above, have you mastered the method of how to implement document update operation in MongoDB? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.