We have mentioned in the previous article the basic document additions and deletions to check the operation, MongoDB in the provision of additions and deletions to check the syntax is very rich, this article we mainly to see what updates have fun grammar.
This is the fourth article in the MongoDB series that understands the previous article to help better understand this article:
Install MongoDB on 1.Linux
2.MongoDB Basic Operation
3.MongoDB data type document replacement
Suppose the following data is now stored in my collection:
{
"_id": ObjectId ("59f005402844ff254a1b68f6"),
"name": "The Chinese Kingdoms",
"AuthorName": "Luo Guan Zhong",
"Authorgender ":" Male ",
" authorage ": 99.0
}
This is a book, with the title and author information, but the author is a separate entity, so I want to extract it and become the following:
{"
_id": ObjectId ("59f005402844ff254a1b68f6"),
"name": "The Chinese Kingdoms",
"author": {
"name": "Luo Guan Zhong",
" Gender ": Male",
"age": 99.0
}
}
I can take the following actions:
Another problem is that when updating, MongoDB will only match the first updated document, assuming that my mongodb has the following data:
{"_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 x 1 data to 99, and it's easy to think of the following command:
Db.sang_collect.update ({x:1},{x:99})
But we found that the result of the execution was this:
{"_id": ObjectId ("59f00d4a2844ff254a1b68f7"), "X":}
{"_id": ObjectId ("59f00d4a2844ff254a1b68f8"), "X": 1}
{"_id": ObjectId ("59f00d4a2844ff254a1b68f9"), "X": 1}
{"_id": ObjectId ("59f00d4a2844ff254a1b68fa"), "X": 2}
That is, only the first matching result has been updated, and nothing else has changed. This is the update rule for MongoDB, which updates only the first matching result. If we want to update all x 1 to x 99, we can use the following command:
Db.sang_collect.update ({x:1},{$set: {x:99}},false,true)
First, we're going to modify the data assigned to set, S e T, Set,set is a modifier, we will explain in detail below, and then two more arguments, the first false means that if the update record does not exist, whether the document we want to update as a new document INSERT, True to insert, False indicates not to insert, default is False, second true indicates whether to update all documents found, False indicates that only the first record is updated, true indicates that all documents found are updated. using the Modifier
Most of the time we modify the document just to change some part of the article, not all of it, but now I'm faced with the question of assuming I'm like the next document:
{X:1,y:2,z:3}
I now want to change the value of x in this document to 99, and I might use the following actions:
Db.sang_collect.update ({x:1},{x:99})
But the update turned out to be like this:
{"_id": ObjectId ("59f02dce95769f660c09955b"), "X": 99}
The following figure:
MongoDB helped me to update the entire document. To solve this problem, we can use the modifier. $set modifier
$set can be used to modify the value of a field and create it if it does not exist. As follows:
If the field does not exist, it is created as follows:
You can also use $unset to delete a field, as follows:
$set can also be used to modify embedded documents, as in the case of the previous book, as follows:
{"
_id": ObjectId ("59f042cfcafd355da9486008"),
"name": "The Chinese Kingdoms",
"author": {
"name": "Luo Guan Zhong",
" Gender ":" Male "
}
}
To modify the author's name, the following actions are:
Db.sang_collect.update ({name: "The Chinese Kingdoms"},{$set: {"Author.name": "Ming Guanzhong"})
The results of the modifications are as follows:
{"
_id": ObjectId ("59f042cfcafd355da9486008"),
"name": "The Chinese Kingdoms",
"author": {
"name": "Ming Dynasty Guanzhong",
"Gender": "Male"
}
}
$inc modifier
$inc is used to increase the value of an existing key, and a new one is created if the key does not exist. For example, I would like to add an age of 99 to the Guanzhong above, in the following way:
Db.sang_collect.update ({name: "The Kingdoms of the},{" $inc: {"Author.age": 99}})
The results of the implementation are as follows:
{"
_id": ObjectId ("59f042cfcafd355da9486008"),
"name": "The Chinese Kingdoms",
"author": {
"name": "Ming Dynasty Guanzhong",
" Gender ": Male",
"age": 99.0
}
}
Add I want to add 1 years to Guanzhong, execute the following command:
Db.sang_collect.update ({name: "The Kingdoms of the},{" $inc: {"Author.age": 1}})
This will add 1 to the existing value, and the result is as follows:
{"
_id": ObjectId ("59f042cfcafd355da9486008"),
"name": "The Chinese Kingdoms",
"author": {
"name": "Ming Dynasty Guanzhong",
"gender": "Male",
"age": 100.0
}
}
Note $inc can only be used to manipulate numbers, not to manipulate null, Boolean, and so on. array modifier
There are several types of array modifier that we look at separately.
$push can append an element to the end of an existing array, create an array if it does not exist, or take our top book as an example, assuming that there is a field in the book, an array that represents the comment on the comments, we can add a comment using the following command:
Db.sang_collect.update ({name: "The Kingdoms of the},{" $push: {comments: "Good Book 666"}})
The comments field does not exist at this time, and the system automatically creates the field for us, with the following results:
{"
_id": ObjectId ("59f042cfcafd355da9486008"),
"name": "The Chinese Kingdoms",
"author": {
"name": "Ming Dynasty Guanzhong",
" Gender ": Male",
"age": 100.0
},
"comments": [
"Good Book 666"
]
}
At this point we can add comments as follows:
Db.sang_collect.update ({name: "The Kingdoms"},{$push: {comments: "Good book 666 La La"})
The results are as follows:
{"
_id": ObjectId ("59f042cfcafd355da9486008"),
"name": "The Chinese Kingdoms",
"author": {
"name": "Ming Dynasty Guanzhong", "
gender": "Male",
"age": 100.0
},
"comments": [
"Good Book 666",
"good book 666 la La la"
]
}
If you want to add 3 comments at a time, you can combine $each together to use, as follows:
Db.sang_collect.update ({name: "The Kingdoms of the},{" $push: {comments:{$each: ["111", "222", "333"]}})
The results are as follows:
{"
_id": ObjectId ("59f042cfcafd355da9486008"),
"name": "The Chinese Kingdoms",
"author": {
"name": "Ming Dynasty Guanzhong",
" Gender ":" Male ",
" age ": 100.0
},
" comments ": [
" Good Book 666 ",
" good book 666 la La la ","
the ","
222 ",
"333"
]
}
We can use $slice to fix the length of the array, assuming that I fixed the length of the array to 5, if the elements in the array are less than 5, then all of them are preserved, and if the elements in the array are more than 5, only the latest 5 will be retained, as follows:
Db.sang_collect.update ({name: "The Kingdoms of the},{" $push: {comments:{$each: ["444", "555"], $slice:-5}}})
Note that the $slice value is negative and the results are as follows:
{"
_id": ObjectId ("59f042cfcafd355da9486008"),
"name": "The Chinese Kingdoms",
"author": {
"name": "Ming Dynasty Guanzhong", "
gender": "Male",
"age": 100.0
},
"comments": [
"A",
"222",
"333",
"444",
"555"
]
}
We can also use $sort to sort the data before we clean it up, and then clean it up like I have a class document as follows:
{"
_id": ObjectId ("59f07f3649fc5c9c2412a662"),
"Class": "Class Two"
}
Now insert student into this document, each student with name and result, and then in descending order of results, as long as the first 5 data are as follows:
Db.sang_collect.update ({class: "Class Two"},{$push: {students:{$each: [{name: "Zhang 100", Score:100},{name: "Zhang 99", score:99},{ Name: "Zhang 98", score:98}], $slice: 5, $sort: {Score:-1}}}})
The $sort takes 1 and 1,-1 to indicate descending, and 1 for ascending.
The above command executes two times (that is, inserted two times), and the result is as follows:
{"
_id": ObjectId ("59f07f3649fc5c9c2412a662"),
"Class": "Class Two",
"students": [
{
"name": "Zhang 100" ,
"score": 100.0
},
{
"name": "Zhang 100",
"score": 100.0
},
{
"name": "Zhang 99",
" Score ": 99.0
},
{
" name ":" Zhang 99 ",
" score ": 99.0
},
{
" name ":" Zhang 98 ",
" score " : 98.0
}
]
}
Slice and S L I c e and slice and sort cannot only and p