MongoDB flatten a deep nested document
with the aggregate
unwind
command
{ $unwind: "$notebooks" },
{ $unwind: "$notebooks.notes"},
{ $project: {
anote: "$notebooks.notes",
_id: 0
}}
the original collection look like this:
[
{
"name": "workshops",
"notebooks": [
{
"name": "FEM1",
"notes": [
{
"name": "intro1",
"contents": "intro description"
},
{
"name": "exercise",
"contents": "exercise solution"
}
]
},
{
"name": "FEM2",
"notes": [
{
"name": "start",
"contents": "start of the course"
},
{
"name": "solution",
"contents": "description of the soluion"
}
]
}
]
},
{
"name": "readinglist",
"notebooks": [
{
"name": "articlenotes",
"notes": [
{
"name": "Objectcreate",
"contents": "object create vs new"
},
{
"name": "blissfulljs",
"contents": "blissfull js library desc"
}
]
}
]
}
]