MongoDB update document (update elements in the array object)

Before we introduced how to update the document, and briefly introduced that when updating the document, you can use options to specify that when the update content does not exist, you can add a new document. For details, please refer to:

MongoDB update document (update a document) https://blog.csdn.net/m1729339749/article/details/129983304
recently encountered a requirement that the document contains an array object, which needs to be updated by filtering elements in the array object that meet the conditions .

1. Prepare data

Add two batches of product data to the batch

db.batch.insertMany([
    { "_id": 1, "foods": [
          { "name": "苹果", "total": "20" },
          { "name": "可口可乐", "total": "30" },
          { "name": "北京方便面", "total": "10" }  
      ] 
    },
    { "_id": 2, "foods": [
          { "name": "伊利纯牛奶", "total": "5" },
          { "name": "可口可乐", "total": "20" },
          { "name": "营养快线", "total": "20" }  
      ] 
    },
]);

Two, arrayFilters

grammar:

db.collection.updateOne(
   <filter>,
   <update>,
   {
     upsert: <boolean>,
     writeConcern: <document>,
     collation: <document>,
     arrayFilters: [ <filterdocument1>, ... ],
     hint:  <document|string>        // Available starting in MongoDB 4.2.1
   }
)

Among them, arrayFilters is used to filter the elements in the array object.

Example: Find the element whose number of nutrient express is 20, and update the number to 30

(1) Query the documents with 20 nutrition express lines

db.batch.find(
    { 
        "foods.name": "营养快线", 
        "foods.total": "20" 
    }
)

The result of the query is as follows:

{
	"_id" : 2,
	"foods" : [
		{
			"name" : "伊利纯牛奶",
			"total" : "5"
		},
		{
			"name" : "可口可乐",
			"total" : "20"
		},
		{
			"name" : "营养快线",
			"total" : "20"
		}
	]
}

(2) Modify the number of nutrition express lines

db.batch.updateOne(
    { 
        "foods.name": "营养快线", 
        "foods.total": "20" 
    }, 
    {
        "$set": { "foods.$[element].total": "30" }
    },
    {
        "arrayFilters": [
            { 
                "element.name": "营养快线", 
                "element.total": "20"
            }
        ]
    }
)

in,

arrayFilters: Represents filtering the elements in the array, and performing an update operation after finding the elements in the array that meet the conditions.

$[element]: represents the filter locator, which is used to locate each array element.

After the operation is performed, the elements in the array will be modified, and the modified document is as follows:

{
	"_id" : 1,
	"foods" : [
		{
			"name" : "苹果",
			"total" : "20"
		},
		{
			"name" : "可口可乐",
			"total" : "30"
		},
		{
			"name" : "北京方便面",
			"total" : "10"
		}
	]
}
{
	"_id" : 2,
	"foods" : [
		{
			"name" : "伊利纯牛奶",
			"total" : "5"
		},
		{
			"name" : "可口可乐",
			"total" : "20"
		},
		{
			"name" : "营养快线",
			"total" : "30"
		}
	]
}

It can be seen from the document that the number of nutrition express lines has been modified to 30

Guess you like

Origin blog.csdn.net/m1729339749/article/details/132268979