0

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:

  1. find the Story;
  2. $lookup for the related Chapters:

    '$lookup': {
        'from': 'chapters',
        'localField': 'idChapter',
        'foreignField': 'idChapter',
        'as': 'chapters'
    }
    
  3. $unwind the Chapters:

    '$unwind': '$chapters'
    
  4. $lookup for the related Images:

    '$lookup': {
        'from': 'images',
        'localField': 'chapters.idImage',
        'foreignField': 'idImage',
        'as': 'images'
    }
    
  5. 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.

shaithana
  • 2,470
  • 1
  • 24
  • 37
  • `'localField': '$chapters'` is not even valid, let alone not being the correct path. You will get more help with an [mcve](https://stackoverflow.com/help/mcve), which basically means "give people enough data to produce the result you expect". So if you want to show a result with multiple chapters, then provide that data and everything actually related to it. – Neil Lunn Apr 19 '18 at 13:03
  • Take a look at https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/#join-conditions-and-uncorrelated-sub-queries. – Alex Blex Apr 19 '18 at 13:18
  • Possible duplicate of [MongoDB nested lookup with 3 levels](https://stackoverflow.com/questions/36019713/mongodb-nested-lookup-with-3-levels) – Alex Blex Apr 19 '18 at 13:18
  • @NeilLunn sorry, I've correct the error, but anyway this is absolutely my real datas. – shaithana Apr 19 '18 at 13:19
  • 1
    I'm not doubting it's "real", just that the example is lacking data. Story has 2 chapters and you provide "one". The provided chapter has two images, and you again provide only "one". Please include the rest of the data that relates back to the original "story". This makes the problem "reproducible" and by definition "solvable". – Neil Lunn Apr 19 '18 at 13:24

0 Answers0