Conditional $lookup on multiple fields in the same document

Hello, so I stumbled on this issue. I have this kind of document:

{
    "day": 1,
    "title": "Lorem ipsum dolor sit amet",
    "data": [
        {
            "type": "body",
            "content": "Lorem ipsum dolor sit amet"
        },
       {
            "type": "content_list",
            "content": [
                {
                    "id": "6312b5bd0fb68141c6bdc4d0",
                },
                {
                    "id": "6311c6c50fb68141c6b97710",
                },
            ]
        },
        {
            "type": "body",
            "content": "Lorem ipsum dolor sit amet"
        },
        {
            "type": "content_list",
            "content": [
                {
                    "id": "6312b5bd0fb68141c6bdc4d0",
                },
                {
                    "id": "6311c6c50fb68141c6b97710",
                },
            ]
        },

There is a list of different content types in data, if the content type is “content_list” then I would like to do a $lookup using the id on another collection.
Is it possible to do a conditional $lookup ?

I believe you could $unwind your data array, then $match on data.type for the value you’re looking for and then perform the necessary join.

It is hard, however, to say if that’s the right approach without seeing data from the other collection and know what the final output your looking for.

2 Likes

I’ve shared the question also on stackoverflow, I leave here the link.

I need the structure to stay the same so there were some more trasformations to be made

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.