In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
For MongoDB databases, changes to data will involve changes in content and structure (including arrays). Therefore, there are a series of modifiers available for MongoDB design. The previously used "$set" is a modifier.
1. $inc: for a number field, add the data content of a number field:
Syntax: {"$inc": {"member": "content"}}
Example: increase the salary of an employee at the age of 30 by 1000, plus 1 year old
> db.emp.update ({"age": 30}, {"$inc": {"sal":-1000, "age": 1}})
WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1})
> db.emp.find ({"age": 31}) .pretty ()
{
"_ id": ObjectId ("599108423268c8e84253be26")
"name": "Zhao Yi"
"sex": "male"
"age": 31
"sal": 8999
"loc": "Beijing"
}
2. $set: reset the content:
Syntax: {"$set": {"member": "New content"}}
Example: change the salary of a person whose age is 30 to 7999
> db.emp.update ({"age": 30}, {"$set": {"sal": 7999}})
WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1})
> db.emp.find ({"age": 30}) .pretty ()
{
"_ id": ObjectId ("599108423268c8e84253be29")
"name": "Li Si"
"sex": "female"
"age": 30
"sal": 7999
"loc": "Beijing"
}
3. $unset: delete the contents of a member:
Syntax: {"$unset": {"members": 1}}
Example: delete the age and salary information of Sun San
> db.emp.find ({"name": "Sun San"}) .pretty ()
{
"_ id": ObjectId ("599108423268c8e84253be28")
"name": "Sun San"
"sex": "male"
"age": 40
"sal": 2000
"loc": "Shenzhen"
}
> db.emp.update ({"name": "Sun San"}, {"$unset": {"age": 1, "sal": 1}})
WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1})
> db.emp.find ({"name": "Sun San"}) .pretty ()
{
"_ id": ObjectId ("599108423268c8e84253be28")
"name": "Sun San"
"sex": "male"
"loc": "Shenzhen"
}
4. $push: equivalent to appending content to the specified member (basically an array):
Syntax: {"$push": {"members": value}}
Example: add 2 course information to Sun San (there is no course information under Sun San information at this time)
> db.emp.update ({"name": "Sun San"}, {"$push": {"course": ["Chinese", "Mathematics"]}})
WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1})
> db.emp.find ({"name": "Sun San"}) .pretty ()
{
"_ id": ObjectId ("599108423268c8e84253be28")
"name": "Sun San"
"sex": "male"
"loc": "Shenzhen"
"course": [
[
"Chinese"
"Mathematics"
]
]
}
Example: add a course information to Li Si (there is no course information under Li Si information at this time)
> db.emp.update ({"name": "Li Si"}, {"$push": {"course": "Chinese"}})
WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1})
> db.emp.find ({"name": "Li Si"}) .pretty ()
{
"_ id": ObjectId ("599108423268c8e84253be29")
"name": "Li Si"
"sex": "female"
"age": 30
"sal": 7999
"loc": "Beijing"
"course": [
"Chinese"
]
}
Example: add an "art" to the course of "Liu A"
> db.emp.find ({"name": "Liu A"}) .pretty ()
{
"_ id": ObjectId ("599129a00184ff511bf02b87")
"name": "Liu A"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
"course": [
"Chinese"
"Mathematics"
"English"
"Music"
"politics"
]
}
> db.emp.update ({"name": "Liu A"}, {"$push": {"course": "Art"}})
WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1})
> db.emp.find ({"name": "Liu A"}) .pretty ()
{
"_ id": ObjectId ("599129a00184ff511bf02b87")
"name": "Liu A"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
"course": [
"Chinese"
"Mathematics"
"English"
"Music"
"politics"
"Art"
]
}
It is used for adding array data. If there is no array, a new array is created, and if there is, the content is added.
5. $pushAll: similar to "$push", you can add more than one content to the array at a time:
Syntax: {"$pushAll": {"member": array contents}}
Example: add multiple course content to Friday
> db.emp.find ({"name": "Friday"}) .pretty ()
{
"_ id": ObjectId ("599108423268c8e84253be2a")
"name": "Friday"
"sex": "female"
"age": 30
"sal": 9999
"loc": "Beijing"
}
> db.emp.update ({"name": "Friday"}, {"$pushAll": {"course": ["Art", "Music"]}})
WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1})
> db.emp.find ({"name": "Friday"}) .pretty ()
{
"_ id": ObjectId ("599108423268c8e84253be2a")
"name": "Friday"
"sex": "female"
"age": 30
"sal": 9999
"loc": "Beijing"
"course": [
"Art"
"Music"
]
}
6. $addToSet: add a new content to the array, which will only be added if the content does not exist
Syntax: {"$addToSet": {member: content}}
Example: add new content to Friday's message
> db.emp.update ({"name": "Friday"}, {"$addToSet": {"course": "dance"}})
WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1})
> db.emp.find ({"name": "Friday"}) .pretty ()
{
"_ id": ObjectId ("599108423268c8e84253be2a")
"name": "Friday"
"sex": "female"
"age": 30
"sal": 9999
"loc": "Beijing"
"course": [
"Art"
"Music"
"Dance"
]
}
At this point, it will determine whether the content to be added exists in the array, append the content to the array if it does not exist, and do not make any modifications if it does.
7. $pop: delete the data in the array
Syntax: {"$pop": {member: content}}. If the content is set to-1, the first one is deleted, and the last one is deleted if the content is set to 1.
Example: delete the first course on Friday
> db.emp.update ({"name": "Friday"}, {"$pop": {"course":-1}})
WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1})
> db.emp.find ({"name": "Friday"}) .pretty ()
{
"_ id": ObjectId ("599108423268c8e84253be2a")
"name": "Friday"
"sex": "female"
"age": 30
"sal": 9999
"loc": "Beijing"
"course": [
"Music"
"Dance"
]
}
Example: delete the last course on Friday
> db.emp.update ({"name": "Friday"}, {"$pop": {"course": 1}})
WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1})
> db.emp.find ({"name": "Friday"}) .pretty ()
{
"_ id": ObjectId ("599108423268c8e84253be2a")
"name": "Friday"
"sex": "female"
"age": 30
"sal": 9999
"loc": "Beijing"
"course": [
"Music"
]
}
8. $pull: deletes a specified content of data from the array
Syntax: {"$pull": {member: data}}, for data comparison, if this data is deleted
Example: delete Wang Wu's music course information
> db.emp.update ({"name": "Friday"}, {"$pull": {"course": "Music"}})
WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1})
> db.emp.find ({"name": "Friday"}) .pretty ()
{
"_ id": ObjectId ("599108423268c8e84253be2a")
"name": "Friday"
"sex": "female"
"age": 30
"sal": 9999
"loc": "Beijing"
"course": []
}
9. $pullAll: delete multiple contents at once
Syntax: {"$pullAll": {member: [data, data,...]}}
Example: delete three courses in Liu A
> db.emp.find ({"name": "Liu A"}) .pretty ()
{
"_ id": ObjectId ("599129a00184ff511bf02b87")
"name": "Liu A"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
"course": [
"Chinese"
"Mathematics"
"English"
"Music"
"politics"
"Art"
]
}
> db.emp.update ({"name": "Liu A"}, {"$pullAll": {"course": ["Chinese", "Mathematics", "English"]}})
WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1})
> db.emp.find ({"name": "Liu A"}) .pretty ()
{
"_ id": ObjectId ("599129a00184ff511bf02b87")
"name": "Liu A"
"sex": "male"
"age": 35
"sal": 8000
"loc": "Beijing"
"course": [
"Music"
"politics"
"Art"
]
}
10. $rename: rename the member name
Syntax: {"$rename": {Old member name: new member name}}
Example: change the name member name of "Sun San" to "name"
> db.emp.find ({"name": "Sun San"}) .pretty ()
{
"_ id": ObjectId ("599108423268c8e84253be28")
"name": "Sun San"
"sex": "male"
"loc": "Shenzhen"
"course": [
[
"Chinese"
"Mathematics"
]
]
}
> db.emp.update ({"name": "Sun San"}, {"$rename": {"name": "name"}})
WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1})
> db.emp.find ({name: "Sun San"}) .pretty ()
{
"_ id": ObjectId ("599108423268c8e84253be28")
"sex": "male"
"loc": "Shenzhen"
"course": [
[
"Chinese"
"Mathematics"
]
]
"name": "Sun San"
}
Throughout the MongoDB database, there is comprehensive support for modifiers.
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.