mongo explains the modification operation in detail

Keywords: Database MongoDB

According to the query criteria, there are also some details to pay attention to in updating, so here we are going to update the details for your own use in the future.

mongodb native

db.<collection>.updateOne(filter, update, [options]); 
db.<collection>.updateMany(filter, update, [options]); 
Copy code

Filter filter condition

Filter is the filter condition of query. Please refer to the previous article for details: mongo advanced - query

options updated content

The following updated content is based on the following data structure:

The second parameter of options determines which fields to update. Its common writing is as follows:

Normal field operation

$set operator

This operation symbol is used to set a single field

// Set the name of the matching document to tenge and address.city to Harbin
{
  $set: { name:"twinkle", "address.province": "Beijing" }
}

// For example: 
db.getCollection('students').updateOne({_id:  ObjectId("6187c759ee4ea6f22d01f45a")},{$set:{name:"twinkle", "address.province": "Beijing" }})
Copy code

$inc

$inc is used to set the self increment of a field

// Set the name of the matching document to twinkle2 and increase its age by 2
{
  $set: { name:"twinkle2" },
  $inc: { age: 2 }
}

// For example:
db.getCollection('students').updateOne({_id:  ObjectId("6187c759ee4ea6f22d01f45a")},{
  $set: { name:"twinkle2" },
  $inc: { age: 2 }
})
Copy code

$mul

Used to set the self multiplication of a field

// Set the name of the matching document to twinkle and multiply its age by 2
{
  $set: { name:"twinkle" },
  $mul: { age: 2 }
}
// This is simple and the same as the one above
 Copy code

$rename

Rename a field of the document from a new name, which does not exist in mysql

// Change the name field of the matching document to fullname
{
  $rename: { name: "fullname" }
}

// For example:
db.getCollection('students').updateOne({_id:  ObjectId("6187c759ee4ea6f22d01f45a")},{
 $rename: { name: "fullname" }
})
Copy code

$unset

Delete a field in the document

// Delete the age field and address.province field of the matching document
{
  $unset: {age:"", "address.province":""}
}

for example:
db.getCollection('students').updateOne({_id:  ObjectId("6187c759ee4ea6f22d01f45a")},{
  $unset: {age:"", "address.province":""}
})
Copy code

Array operation

The array operation in mongo is different, and there will be special instructions to modify it

$addToSet

If the array does not exist, add it. If it does exist, no operation will be performed

// Add a: code to loves
{
  $addToSet: {
    loves: "code"
  }
}

// for example
db.getCollection('students').updateOne({_id:  ObjectId("6187c759ee4ea6f22d01f45a")},{
 $addToSet: {
    loves: "code"
  }
})
Copy code

$push

Adding a data item to an array, whether it exists in the array or not, is bound to be added

{
  $push: {
    loves: "code"
  }
}
// This is the same as the above. They are all added
 Copy code

$each

Add multiple items to the array

{
  $push: {
    loves: { $each: ["game", "game2"]}
  }
}
// for example
db.getCollection('students').updateOne({_id:  ObjectId("6187c759ee4ea6f22d01f45a")},{
  $push: {
    loves: { $each: ["game", "game2"]}
  }
})
Copy code

$pull

Delete one or more items of the array

// Delete love's code,game
{
  $pull: {
    loves: {$in: ["code","game"]}
  }
}

// For example:
db.getCollection('students').updateOne({_id:  ObjectId("6187c759ee4ea6f22d01f45a")},{
   $pull: {
    loves: {$in: ["code","game"]}
  }
})
Copy code

.$

Modify an item in the array

// Change game2 in all loves to game
// This operator needs to be used with query criteria
db.students.updateOne({
   _id:  ObjectId("6187c759ee4ea6f22d01f45a")
  loves: "game2"
}, {
  $set: {
    "loves.$": "game"
  }
})
Copy code

For more operators, see: docs.mongodb.com/manual/refe...

Other configurations

The third parameter is other configurations

  • upsert: the default is false. If a match cannot be found, it will be added
// I changed the id a to c, which is not in the database
db.students.updateOne({
   _id:  ObjectId("6187c759ee4ea6f22d01f45c"),
}, {
  $set: {
    "loves": "game"
  }
},{
upsert: true
})
Copy code

mongoose

Method 1: update directly with function

<Model>.updateOne(filter, doc, [options]);
<Model>.updateMany(filter, doc, [options]);
Copy code

Method 2: update in the model instance and save

const u = await Students.findById("6187c759ee4ea6f22d01f45c");
u.address.province = "Beijing";
u.happys.push("game", "code");
await u.save(); // The old and new documents will be automatically compared to complete the update
 Copy code

The difference between this approach and native:

  • _ IDS can be matched directly using strings
  • You can omit $set in doc and change it directly (you can modify it directly with js code)
  • By default, validation will not be triggered. You need to set runValidators: true in the options of the model to enable validation

Posted by polybiosis on Tue, 09 Nov 2021 17:08:55 -0800