0

I have and object array as follows:

project: = {
    id: 111,
    title: "Project TEST",
    tabs: [
      {
        tabId: 123,
        products: [
          {
            uniqId: 1,
            price: 180
            quantity:5
          },
          {
            uniqId: 2,
            price: 80
            quantity:2
          },
        ]
      },
      {
        tabId: 50,
        products: [
          {
            uniqId: 11,
            price: 180
            quantity:5
          },
          {
            uniqId: 12,// How I Identify this product inside the tab
            price: 80 // I want to update all this Product Object
            quantity:2 // I want to update all this Product Object
          },
        ]
      }
    ]
  }

Given a project ID = 111, a tabId = 50, and a product uniqId = 12, I want to update all the object Product. First I found the Project with findOne. Then I have to get the tab and the products. I have used arrayFilters of mongose docs. It doesn't update the project in database.

How I can update this nested object array in the document?

async updateTabProduct(projectId,bodyProductToUpdate){
    try{
      const { Project: projectSchema } = this.getSchemas();
      const projec0t = await projectSchema.findOneAndUpdate(
      {
          "_id": projectId,
          "tabs": {
              "$elemMatch": {
                  tabId, "products.uniqId": body.uniqId
              }
          }
      },
      { "$set": { 
          "tabs.$[outer].products.$[inner].notes:": body.notes,
          "tabs.$[outer].products.$[inner].nom:": "asdfafd",
      } },
      { "arrayFilters": [
          { "outer.tabId": body.tabId },
          { "inner.uniqId": body.uniqId }
      ] }, (err, result) => {
      if (err) {
          console.log('Error updating service: ' + err);
      } else {
      }
  }); 
  return projec0t;
 }
    catch(error) {
      throw error;
    }
  }
Arnau Bosch
  • 73
  • 1
  • 8
  • Here is one to update documents with nested arrays: [Updating nested array inside array mongodb](https://stackoverflow.com/questions/29634150/updating-nested-array-inside-array-mongodb#:~:text=With%20MongoDB%203.6%20and%20above,oid%2C%20pid%20%7D%20%3D%20req.). In general, to update array elements use the [arrayFIlters](https://docs.mongodb.com/v4.2/reference/method/db.collection.updateMany/#updatemany-arrayfilters) option of the _update_ method along with the [Array Update Operators](https://docs.mongodb.com/v4.2/reference/operator/update/index.html#array). – prasad_ Feb 01 '21 at 10:11
  • It doesn't update the Project. I have edited the question with my code – Arnau Bosch Feb 01 '21 at 10:37
  • @ArnauBosch For mongoose syntax specifically see https://stackoverflow.com/questions/30963685/mongoose-cannot-update-using-positional-operator , the working code is in the last update of the question itself – Alex Blex Feb 01 '21 at 10:41
  • @ArnauBosch You can try this approach as shown in this post [Node.js Mongoose .update with ArrayFilters](https://stackoverflow.com/questions/48215167/node-js-mongoose-update-with-arrayfilters) – prasad_ Feb 01 '21 at 10:53

0 Answers0