I want to create a mongodb document structure like below.
data:[[{a:b},{b:c}],[{e:f}],[{f:g},{j:h},{i:l}]]
This structure will allow me to add new sub array element, 4th one to the existing data array which contains three sub arrays in the above example, if required.
I am able to add new sub array element using below command from mongodb shell.
db.xyz.update({'id':'A01'},{$push:{data:[]}},{})
However, I am unable to findout solution for pushing data in to respective arrays. The following command is not working.
db.xyz.update({'id':'A01'},{$push:{data[0]:{$push:{a:b}}}},{})
A single $push should be enough. Try this instead:
db.xyz.update({'id':'A01'}, {$push: {"data.0": {a: "b"}}})
result:
{
"_id":ObjectId("586b907a4979a26deaacf4ad"),
"data":[
[
{
"a":"b"
},
{
"b":"c"
},
{
"a":"b"
}
],
[
{
"e":"f"
}
],
[
{
"f":"g"
},
{
"j":"h"
},
{
"i":"l"
}
]
]
}