LUKER
LUKER

Reputation: 536

$push to multiple nested array two deep

Here is what my current collection looks like:

Object: foods,
Group: {
    Name: fruit
    Kinds: {
       Name: Apple,
       Details: 
           {
           Name: Honey Crisp,
           Color: red,
           Taste: sweet
           }
       
    }
    Name: Meat
    Kinds: {
       Name: Pork,
       Details: 
           {
           Name: Pork Chop,
           Color: red,
           Taste: sweet
           }
           {
           Name: Bacon,
           Color: red,
           Taste: salty
           }
       
    }
}

So I'd like to add another type of apple into the details document. I've seen people add documents one layer deep with $push, but I have not seen any tutorials on how to add a document more than one layer deep. Please help.

EDIT I'd like to add this under the details of apple without using indexes.

          {
           Name: Red Delicious,
           Color: red,
           Taste: Sweet
           }

Upvotes: 0

Views: 218

Answers (1)

Martin Hoang
Martin Hoang

Reputation: 74

This is wrong on so many levels, please next time use [] for arrays, use , between fields.

Answer for your question is arrayFilters . With them you can you do magic, you are looking for.

Upvotes: 1

Related Questions