Update nested array in collection

I have the following collection:

Collection = 
{
  _id: "TYcV70JNDCZ4Kk72ut",
  text: {
            [{
              [{
                 textId: 123,
                posted: 0
               },
               {
                 textId: 1234,
                 posted: 0
               }]
            }],
            [{
              [{
                 textId: 78,
                posted: 0
               },
               {
                 textId: 789,
                 posted: 0
               }]
            }]
         }

}

So, I have a collection that has an array called text and each element of text is also an array. How would I change the values of the item posted within the inner most array if the textId is for example 123?

This is what I tried so far and it hasn’t worked out:

Collection.update({_id: "TYcV70JNDCZ4Kk72ut", "text.textId": 123},
  {$set: {"text.posted": 1}});

Collection.update({_id: “TYcV70JNDCZ4Kk72ut”, “text.textId”: 123}, {$set: {“text .$. posted”: 1}});

Hey! Thanks for that. I realized the array within array within another array was probably not a good structure and restructured the collection to make it work!

Not necessarily. That depends on what you are doing with nested data. If you always need to query nested data together with parent data it makes sense. But sure it doesn’t make updates easy.

1 Like