The use of modifiers in MongoDB
In the process of reusing Mongodb, usually only part of the document needs to be updated, and you can use the atomic update modifier in MongoDB. Update modifiers are special keys that specify complex update operations, such as modifying, adding, or deleting keys, as well as manipulating arrays or embedded documents.
(1) $inc: used to increase the value of an existing key, or if the key does not exist, create one.
Suppose a collection holds the analysis data of the site, which contains the number of page clicks. The requirement is that each click increases the number of page clicks by 1, which can be achieved using "$inc".
> db.site.findOne ()
{
"_ id": ObjectId ("589bc80fd856931885c3dc40")
"url": "www.example.com"
"pageviews": 55
}
Every time someone visits the page, they find the page through URL and increase the value of "pageviews" with the "$inc" modifier.
> db.site.update ({"url": "www.example.com"}, {"$inc": {"pageviews": 1}})
Now, perform a find operation to find that the value of "pageviews" has increased by 1.
> db.site.findOne ()
{
"_ id": ObjectId ("589bc80fd856931885c3dc40")
"url": "www.example.com"
"pageviews": 56
}
Note: the value of "_ id" cannot be changed when using the modifier (note that the value of "_ id" can be changed when the entire document is replaced). Other key values, including those of other unique indexes, can be changed.
The value of the'$inc 'key must be numeric and cannot use strings, arrays, or other non-numeric values.
(2) $set: used to specify the value of a field. If this field does not exist, create it. This is convenient for updating schemas or adding user-defined keys.
Example: user information is stored in the following document:
> db.user.findOne ()
{
"_ id": ObjectId ("589bcbe3d856931885c3dc41")
"age": 30
"location": "xi'an"
"name": "joe"
"sex": "male"
}
If you want to add books that users like, you can use the "$set" implementation:
> db.user.update ({"_ id": ObjectId ("589bcbe3d856931885c3dc41")}, {"$set": {"favorite book": "war and peace"}})
Now, by performing a find operation, you can find that the key "favorite book" already exists.
> db.user.findOne ()
{
"_ id": ObjectId ("589bcbe3d856931885c3dc41")
"age": 30
"favorite book": "war and peace"
"location": "xi'an"
"name": "joe"
"sex": "male"
}
"$set" can also modify the value of the key. If the user's favorite book becomes another, you can use "$set" to modify it:
> db.user.update ({"_ id": ObjectId ("589bcbe3d856931885c3dc41")}, {"$set": {"favorite book": "Green Eggs and Ham"}})
> db.user.findOne ()
{
"_ id": ObjectId ("589bcbe3d856931885c3dc41")
"age": 30
"favorite book": "Green Eggs and Ham"
"location": "xi'an"
"name": "joe"
"sex": "male"
}
"$set" can even change the type of key. For example, if users have many favorite books, you can change the value of the "favorite book" key into an array:
> db.user.update ({"name": "joe"}, {"$set": {"favorite book": ["cat's cradle", "foudation trilogy", "ender's name"]}})
> db.user.findOne ()
{
"_ id": ObjectId ("589bcbe3d856931885c3dc41")
"age": 30
"favorite book": [
"cat's cradle"
"foudation trilogy"
"ender's name"
]
"location": "xi'an"
"name": "joe"
"sex": "male"
}
(3) $unset: delete key
If users suddenly find that they don't really like reading, they can use "$unset" to delete the key completely:
> db.user.update ({"name": "joe"}, {"$unset": {"favorite book": 1}})
> db.user.findOne ()
{
"_ id": ObjectId ("589bcbe3d856931885c3dc41")
"age": 30
"location": "xi'an"
"name": "joe"
"sex": "male"
}
$set can also modify embedded documents:
> db.blog.posts.find ()
{
"_ id": ObjectId ("589bdb7ed856931885c3dc42")
"title": "A blog post"
"content": "blog text"
"author": {
"name": "joe"
"email": "joe@example.com"
}
}
> db.blog.posts.update ({"author.name": "joe"}, {"$set": {"author.name": "zhangyahui"}})
> db.blog.posts.findOne ()
{
"_ id": ObjectId ("589bdb7ed856931885c3dc42")
"author": {
"email": "joe@example.com"
"name": "zhangyahui"
}
"content": "blog text"
"title": "A blog post"
}
When adding, modifying, or deleting keys, you should use the $modifier.
(4) $push: you will want to add an element to the end of an existing array, and if not, create a new array.
> db.blog.posts.findOne ()
{
"_ id": ObjectId ("589bdb7ed856931885c3dc42")
"author": {
"email": "joe@example.com"
"name": "zhangyahui"
}
"content": "blog text"
"title": "A blog post"
}
> db.blog.posts.update ({"title": "A blog post"}, {"$push": {"comments": {"name": "joe", "email": "joe@example.com", "content": "nice post"})
> db.blog.posts.findOne ()
{
"_ id": ObjectId ("589bdb7ed856931885c3dc42")
"author": {
"email": "joe@example.com"
"name": "zhangyahui"
}
"comments": [
{
"name": "joe"
"email": "joe@example.com"
"content": "nice post"
}
]
"content": "blog text"
"title": "A blog post"
}
If you want to add another comment, continue to use "$push".
(5) $each: using the "$each" sub-operator, you can add multiple values through a "$push" operation:
> db.blog.posts.findOne ()
{
"_ id": ObjectId ("589bdb7ed856931885c3dc42")
"author": {
"email": "joe@example.com"
"name": "zhangyahui"
}
"comments": [
{
"name": "joe"
"email": "joe@example.com"
"content": "nice post"
}
]
"content": "blog text"
"hourly": [
five hundred and forty three
]
"title": "A blog post"
}
> db.blog.posts.update ({"title": "A blog post"}, {"$push": {"hourly": {"$each": [562.776562.790559.123]})
> db.blog.posts.findOne ()
{
"_ id": ObjectId ("589bdb7ed856931885c3dc42")
"author": {
"email": "joe@example.com"
"name": "zhangyahui"
}
"comments": [
{
"name": "joe"
"email": "joe@example.com"
"content": "nice post"
}
]
"content": "blog text"
"hourly": [
five hundred and forty three,
562.776
562.79
559.123
]
"title": "A blog post"
}