I have three collections:
Stories:
{
"idStory": "story-5ac97232b2ea82701e853d0b",
"chapters": [
{ "idChapter": "chapter-5ac97274b2ea82701e853d11" },
{ "idChapter": "chapter-5ac97593c9c35a7079f85f8c" }
]
}
Chapters:
[
{
"idChapter": "chapter-5ac97274b2ea82701e853d11"
},
{
"idChapter": "chapter-5ac97593c9c35a7079f85f8c",
"images": [
{ "idImage": "image-5ac97593c9c35a7079f85f8c" },
{ "idImage": "image-5ac9f63f8bfb1b7b5bced68e" }
]
}
]
Images:
[
{
"idImage": "image-5ac97593c9c35a7079f85f8c",
"URI": "http://mypath1"
"dateCreation": "12/12/1973"
},
{
"idImage": "image-5ac9f63f8bfb1b7b5bced68e",
"URI": "http://mypath2"
"dateCreation": "12/12/1973"
}
]
And I need this result:
{
"idStory": "story-5ac97232b2ea82701e853d0b",
"chapters": [
{ "idChapter": "chapter-5ac97274b2ea82701e853d11" }
{
"idChapter": "chapter-5ac97593c9c35a7079f85f8c"
"images": [
{ "URI": "http://mypath1" },
{ "URI": "http://mypath2" }
]
}
]
}
I've tried with this stages:
find
the Story;$lookup
for the related Chapters:'$lookup': { 'from': 'chapters', 'localField': 'idChapter', 'foreignField': 'idChapter', 'as': 'chapters' }
$unwind
the Chapters:'$unwind': '$chapters'
$lookup
for the related Images:'$lookup': { 'from': 'images', 'localField': 'chapters.idImage', 'foreignField': 'idImage', 'as': 'images' }
and finally
$project
:'$project': { 'idStory': true, 'chapters': { 'idChapter': '$chapters.idChapter', 'images': { 'URI': '$images.URI' } } }
but this doesn't works: it always repeat the first element on the unwinded array.
Any help will be appreciated.