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
            }]
        }
    ]
}
1
  • Can you edit this question with required o/p for the given doc & request Commented Feb 28, 2020 at 5:49

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.