This is the 20th day of my participation in the Genwen Challenge

The biggest difference between document-type database and relational database like MongoDB is that all data is stored according to documents, so the update involves deep data update, such as how to update the subordinate object attributes of an object. This article describes how to update data for multilevel documents.

Nested array updates

Take the following data (data set named author) as an example:

{name: 'jiji ', Documents: [' start Flutter ',' high performance MySQL', 'MongoDB professional refers to north ']}Copy the code

We need to change the MongoDB professional north to MongoDB professional north, which is to modify the nested document data. In this case, use the location operator $provided by MongoDB. The location operator represents the data matching the corresponding conditions, such as:

Db.author. update({name: 'MongoDB ', documents: 'MongoDB ', {'documents.$': {'documents. ': 'MongoDB ');Copy the code

The same is true for arrays of children.

{
  name: '岛上码农', 
  documents: [
    {name: 'Flutter入门与实战', score: 80}, {name: 'high performance MySQL', score:90}, {name: 'MongoDB ', score:85}}]Copy the code

You need to modify the name and score of the documengs nodes.

Db.author. update({name: 'MongoDB ', 'Documents. Name ': 'MongoDB '}, {' documents. 'MongoDB is not professional ', 'Documents.$.score':88}});Copy the code

The $location operator is the location of the data element in the array found in the query condition, that is, it is operating on the data of that location.

Update the attributes of the child document

You can use the property accessor “directly when updating the attributes of a child document. For example, the following need to update praise to be increased to 291.

{name: 'scores ', scores: {view:18800, 
    praise: 290, 
    followers: 105}}Copy the code
The db. The author. The update ({name: farmers' island of '}, {' $set: {' scores. Praise:291}});Copy the code

If there is a nested array in the subordinate document, the operation is similar. The “public account” needs to be changed into “wechat public account” for the following data:

{name: 'scores ', scores: {view:18800, 
    praise: 290, 
    followers: 105, platform: [' gold ', 'public ']}}Copy the code
Db. The author. The update ({' name ':' farmer on the island of ', 'scores. The platform' : 'public number'}, {' $set: {' scores. Platform. $' : 'WeChat public number'}});Copy the code

Attributes added and removed

MongoDB provides $push and $pull operations to add or remove attributes, as well as $POP to remove the first or last value of an array. We add a homepage property to the previous document.

Db.author. update({name: 'PHP '}, {$push: {homepage:' HTTPS://juejin.cn/user/70787819648695'}}
);
Copy the code

Notice that we have inserted an array named homepage, one of which is: juejin.cn/user/707878… . To increment a property that is not an array, use the $set directive. You can use pull to remove one of the matched attributes.

Db.author. update({name: 'PHP '}, {$pull: {homepage:' HTTPS://juejin.cn/user/70787819648695'}}
);
Copy the code

The $pop operation uses the numbers -1 and 1 to remove the first and last elements. For the following data, remove the array platform.

{
  name: '岛上码农',
	scores : {
		view: 18800,
		praise: 290,
		followers: 105, platform: [' Nuggets ', 'public ',' others'1', 'the other2']}}Copy the code
// Remove the first elementDb.author. update({name: 'scores'), {$pop: {'scores. Platform ':- 1}});
// Remove the last elementDb.author. update({name: 'scores'), {$pop: {'scores. Platform ':1}});
Copy the code

conclusion

This article introduces MongoDB’s nested attribute update operation, focusing on the use of the location operator $. Through the locator can locate the location of the data we need to modify, and then update the operation. $push, $pull, and $POP instructions can be used to add and remove attributes.