How to update value of specific embedded document, inside an array, of a specific document in MongoDB?
-
30-10-2019 - |
Question
I have the following structure in my document:
{
_id : ObjectId("43jh4j343j4j"),
array : [
{
_arrayId : ObjectId("dsd87dsa9d87s9d7"),
someField : "something",
someField2 : "something2"
},
{
_arrayId : ObjectId("sds9a0d9da0d9sa0"),
someField : "somethingElse",
someField2 : "somethingElse2"
}
]
}
I want to update someField and someField2 but only for one of the items in the array, the one that matches _arrayId (e.g. _arrayId : ObjectId("dsd87dsa9d87s9d7")
; and only for this document (e.g. _id : ObjectId("43jh4j343j4j")
) and no other.
The arrayIds are not unique to the document that's why I need it to be for a specific document. I could use the $ positional operator
if I wanted to update that value within the array for every document it exists in, but that's not what I want.
I am trying to accomplish this in java but a command line solution would work as well.
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow