0

Below is my nested array and I want to update a value in inner array. The scenario is similar to this solution

But, I have only inner array inside first array. So , I am not sure how to specify the identifier for the first array

{
    "RequestID": "A22A8D43FE5D8D1409D5984003AB5609",
    "OPC_REQUEST_ID": [{
            "B69BF2DCEBB932A2665920BE14BA14C5": [{
                "Message": "Some message ",
                "Time": "2020-02-25T07:30:52Z",
                "bug": [
                    "27571088",
                    "7452257"
                ],
            "label": [
                    "No Label"
                ],
                "score": -0.5106
            }]
        },
        {
            "8E0FCBBF92D7226ADB6F3DB465DDA7F4": [{
                "Message": "Some message",
                "Time": "2020-02-25T07:31:52Z",
                "bug": [],
                "label": [
                    "No Label"
                ],
                "score": -0.7184
            }]
        }
    ]
}

I tried below query, but the key for the identifier "j" is another array , so I am not sure how specify key/value for "j" in the "arrayFilters"

db.requestid.update({'RequestID':'A22A8D43FE5D8D1409D5984003AB5609'},{'$set':{'OPC_REQUEST_ID.$[j].8E0FCBBF92D7226ADB6F3DB465DDA7F4.$[k].score' : '0'}},
{arrayFilters:[{'j.OPC_REQUEST_ID' :'8E0FCBBF92D7226ADB6F3DB465DDA7F4' }, {'k.Message' : 'Some message'}]})
}

I want to edit the "score" in the second array from -0.7184 to 0 . So the output would become

{
    "RequestID": "A22A8D43FE5D8D1409D5984003AB5609",
    "OPC_REQUEST_ID": [{
            "B69BF2DCEBB932A2665920BE14BA14C5": [{
                "Message": "Some message ",
                "Time": "2020-02-25T07:30:52Z",
                "bug": [
                    "27571088",
                    "7452257"
                ],
            "label": [
                    "No Label"
                ],
                "score": -0.5106
            }]
        },
        {
            "8E0FCBBF92D7226ADB6F3DB465DDA7F4": [{
                "Message": "Some message",
                "Time": "2020-02-25T07:31:52Z",
                "bug": [],
                "label": [
                    "No Label"
                ],
                "score": 0
            }]
        }
    ]
}
Binoy Thomas
  • 299
  • 3
  • 8

1 Answers1

1

Your example document has Message: "Some message" and Message: "Some message " - not very obvious.

Anyway, this might be the solution:

db.collection.updateMany(
   { RequestID: 'A22A8D43FE5D8D1409D5984003AB5609' },
   { $set: { 'OPC_REQUEST_ID.$[].8E0FCBBF92D7226ADB6F3DB465DDA7F4.$[k].score': 0 } },
   { arrayFilters: [{ 'k.Message': 'Some message' }] }
)

You use A22A8D43FE5D8D1409D5984003AB5609 as a key, I don't think this is a optimal design.

Wernfried Domscheit
  • 54,457
  • 9
  • 76
  • 110