Mongo Db - update element by id inside nested arrays - java spring mongo data

51 Views Asked by At

Trying to update element by id = 1234567890 and id= 8888888888 and .. (bulk) inside nested array es in the example below

{
  "name": "template",
  "process": [
    {
      "identifier": "process1",
      "steps": [
        {
          "identifier": "step1",
          "sections": [
            {
              "identifier": "section1",
              "wrappers": [
                {
                  "id: ": "000000000",
                  "item": {
                    "internalId": "00000001",
                    "name": "q1",
                    
                  }
                },
                {
                  "id: ": "1234567890",
                  "item": {
                    "internalId": "00000002",
                    "name": "q2",
                    "question": "why"
                  },
                  "answer": "**update answer here**"
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

So far I tried to do like but unfortunately this syntax not acceptable :

db.collection.update(
    { "process.$.steps.$.sections.$.wrappers.id": "1234567890" },
    { "$set": { "process.$.steps.$.sections.$.wrappers.answer": "my answer fooo XXX " } }
)

I prefer using bulk , and also working with java - spring mongo data

please suggest

1

There are 1 best solutions below

2
Pavel Bely On BEST ANSWER

Maybe a bit ugly but it works

db.collection.update({
  "process.steps.sections.wrappers.id": "1234567890"
},
{
  "$set": {
    "process.$[process].steps.$[step].sections.$[section].wrappers.$[wrapper].answer": "my answer fooo XXX "
  }
},
{
  arrayFilters: [
    {
      "process": {
        "$exists": true
      },
      
    },
    {
      "step": {
        "$exists": true
      }
    },
    {
      "section": {
        "$exists": true
      },
      
    },
    {
      "wrapper.id": "1234567890"
    }
  ]
})

A bit more meaningful variant:

db.collection.update({
  "process.steps.sections.wrappers.id": "1234567890"
},
{
  "$set": {
    "process.$[process].steps.$[step].sections.$[section].wrappers.$[wrapper].answer": "my answer fooo XXX "
  }
},
{
  arrayFilters: [
    {
      "process.identifier": "process1",
    },
    {
      "step.identifier": "step1",
    },
    {
      "section.identifier": "section1",
    },
    {
      "wrapper.id": "1234567890"
    }
  ]
})

Mongo Playground: https://mongoplayground.net/p/yQyDBN7y2N7

And finally - Bulk Write:

db.collection.bulkWrite([
{ updateOne :
      {
         "filter": { "process.steps.sections.wrappers.id": "1234567890" },
         "update": {
            "$set": { "process.$[process].steps.$[step].sections.$[section].wrappers.$[wrapper].answer": "my answer fooo XXX "
           }
         },
         arrayFilters: [
           {
             "process.identifier": "process1",
           },
           {
             "step.identifier": "step1",
           },
           {
             "section.identifier": "section1",
           },
           {
             "wrapper.id": "1234567890"
           }
         ]
      }
   },
   // another update
])

A minor thing, but you have a typo in your example data, id has extract double quote and semicolon - "id: "