im trying to update an array inside an object but i didnt find the right way to do this.
my document looks like this:
{
"_id" : ObjectId("62792b4a0c9c5a00b6a8e17b"),
"username" : "user_1",
"words" : [
{
"word" : "RATIONAL",
"subwords" : [
"RAT",
"TAN"
]
},
{
"word" : "YOUNGER",
"subwords" : [
"YOU",
"YOUR"
]
}
]
}
i want to push to subwords array with a specific word for example i want to push "AT" to the subwords of "RATIONAL"
thanks for the help 🙂
2
Answers
You can use
filter()
javascript function to get "RATIONNAL" and then push the desired value into the subwords array.Example supposed that there is only ONE word RATIONNAL and that it exists. If it not the case, you need to improve this code to deal with all cases.
Running this will print this to console:
You can use arrayFilters update option as follow:
Explained:
Define arrayFilter x to match the word entry in which you will push the AT value to subwords array
Playground