By default, the MONGO modification modifies only the first data that is found, and if you want to modify all the records that the query matches, you must use the multi parameter.
Description of the modified operator:
$inc Grow a field with the given value;
$set replace the given key value;
$push If the field is an array, the given value will be added to the array field, and if it does not exist, it will be added automatically if the field is non-array, the error will be reported;
$PUSHALL is similar to push, except that the parameter is an array;
$unset Delete a field
$addToSet is similar to push, except that if the value already exists, it is not added;
$pop remove the first or last value of an array field, differentiated by 1 or 1;
$pull If the field is an array, you can use this operator to remove the value that satisfies the condition in the divisor group;
$pullAll is similar to pull, except that the parameter is an array;
$rename change the name of the field;
Instance:
Achieve the final result: Users (collection)
[JavaScript]View PlainCopy
- {
- _id:objectid ("50897dbb9b96971d287202a9"),
- Name: "Jane",
- AGE:20,
- Likes: [ "Tennis", "Golf"],
- Registered: false,
- Addr: {
- City: "Lyon",
- Country: "France"
- }
- }
In order to practice the different commands of update, first insert the basic
[JavaScript]View PlainCopy
- Db.users.insert ({name:"Jane", age:10})
Db.users.update (Query,obj,upsert,multi)
The parameters are query criteria {name: "Jane"}; obj changing the object; Upsert Update or Insert (Boolean); multi (if multiple lines are updated)
Enter db.users.update in the command to see the implementation logic of the update function directly
The actual upsert can be object {upsert:true,multi:true}
1, $inc for a field to increase the specified value value=old+inc; Action fields are not allowed as numbers
[JavaScript]View PlainCopy
- Db.users.update ({name:"Jane"},{$inc: {age:10}})
2, $set replace the original field value
[JavaScript]View PlainCopy
- Db.users.update ({name:"Jane"},{$set: {age:20}})
3, $push, $pushAll Add data to the field array
[JavaScript]View PlainCopy
- Db.users.update ({name:"Jane"},{$pushAll: {like:["Golf"}})
The rest of the instructions are a reason, and I'm not going to write anymore.
Finally, there is a problem, that is, addr field, if I only add a city: "Lyon", then County if the update it?
Similarly, the addr field is missing a county: "France", how do I add it?
Let me check the data and fix it.
Python Operation MongoDB Instruction