MongoDB Basic Operation Update
Update method parameters
// query: Conditions for Updates // obj: Updated object // Boolean type, true: No new, false: Update only not new // Multi: Whether to allow bulk updates function (query, obj, Upsert, multi) { //dosomething}
View Code
Document substitution:
var where={ "age": +}; var p1 = db.user.findOne (where);p 1.age+ +; // age Self-increment 1db.user.update (WHERE,P1); // Replace the first document that matches to a new document
View Code
Common modifiers:
- $set modifier: The $set modifier is used to modify the value of a field in a document, which is added if the field does not exist.
var where={ "_id": ObjectId ("5634b21827480baffb199df4")}; var action={ "$set": {"hobby": "Run"}//$set modifier Updates or adds the specified field } Db.user.update (where,action); // Add a hoppy field for Deng Chao
View CodeModify the type of a field
var where={ "_id": ObjectId ("5634b21827480baffb199df4")}; var action={ "$set": {"hoppy": ["Run", "Sing", "Dance"]}// Change the previous hoppy field to an interest array }db.user.update (where,action);
View CodeModify fields in an inline document
varWhere={ "_id": ObjectId ("5634b21827480baffb199df4")};varaction={ "$set": { "Family": { "Wife": "Sun Li", "Father": "Unknown", "Monther": "Unknown", "Sons": ["Deng Han", "Deng Han One"]}}} ;d b.user.update (where,action);//added Deng's super family relationship dataaction={ "$set": {"Family.father": "Deng"}//Change the Father field in the Deng Hyper Relationship document to Deng}db.user.update (where,action);
View CodeDelete a field from a document
var where={ "_id": ObjectId ("5634b21827480baffb199df4")}; var action={ "$unset": {"Family.wife": 1}//$unset operator for removing a field }; Db.user.update (where,action);
View Code
- $inc modifier: self-increment or decrement modifier, atomic operation.
var where={ "_id": ObjectId (" 5634b21827480baffb199df4 ")}; var action={ "$inc": {"Age": 1}// Age auto-increment 1,-1 is self-decrement, the operator can only be used for numeric types, otherwise it will be abnormal
view code
- $push Operator: Adds an element to the end of an array, and if the specified array does not exist, the array is created
var Where={ "_id": ObjectId ("5634b21827480baffb199df4" var action={ "$push": {"family.sons": " Deng Zi "}// add an element to the end of the sons array, and if the sons array does not exist, it will be created
view code
- $each Operator: The $push operator with the $each operator enables you to add multiple elements to an array at once
var Where={ "_id": ObjectId ("5634b21827480baffb199df4" var action={ "$push": {"family.sons": {" $each ": [" Deng Zi qi "," Dengxiao "," Deng Jiu Mei "]}}//
view code
- $slice operator: Used to set the length of the array, when the value is positive, only the first n elements in the array are preserved, negative, the last n elements in the array are preserved
var Where={ "_id": ObjectId ("5634b21827480baffb199df4" var action={ "$push": {"family.sons": {" $each ": [" Deng Zi qi "," Deng Han One "," Deng Han "]," $slice ": -2}}//
view code
- $sort Operator: Sort elements in an array
{ "_id": ObjectId ("5634b21827480baffb199df4" var action={ "$push": {"family.sons": {" $each ": [" Deng Zi qi "," Deng Han One "," Deng Han "]," $sort ": -1}}// ";d b.user.update (where,action);
view code
- $addToSet Operator: Adds a new element to the array and only adds
var where={ "_id": ObjectId ("5634b21827480baffb199df4" var action={ "$addToSet": {"family.sons ": {" $each ": [" Deng Zi qi "," Deng Han XI "]," $sort ": -1}}// ";d B.user.update (where,action) only if the element is not in the array;
view code
- $pop Operator: Removes an element from the top or tail of an array, which can be used to simulate a queue or stack
var where= { "_id": ObjectId ("5634b21827480baffb199df4" ) }; var action={ "$pop": {"family.sons":-1} //
view code
- $pull operator: Deletes all matching elements in the array
var where={ "_id": ObjectId ("5634b21827480baffb199df4")}; var action={ "$pull": {"family.sons": "Deng Han One"}// Delete all specified elements in the specified array }; Db.user.update (where,action);d B.runcommand ("GetLastError");
View Code
- To modify an element by the position of an array element:
var where={ "_id": ObjectId ("5634b21827480baffb199df4")}; var action={ "$inc": {"Age": -5},// 5 "$set": {"family.sons.0": "Deng Zi qi" "},// Modify the value of the first element in the sons array directly by subscript: Deng Zi ;d b.user.update (where,action);
View CodeUse the locator:
var where={ "family.sons.name": "Deng Zi qi"}; The var action={ //$ symbol is automatically positioned to match the array elements, such as the first match to the array element subscript 3, then $ can actually be considered to be 3 "$ Set ": {" Family.sons.$.name ":" Dengxiao "},};d b.user.update (where,action);
View Code
- findandmodify function: Update and return a document
db.user.findAndModify ({ // query condition // Sort method update: {$set: { Name: ' Zhang Jie '}, $inc: {age:2}},// Update, or use Remove:true to execute delete new:true// Returns the updated document });
View Code
MongoDB Learning Notes (ii)