'Mongodb aggregation using match and lookup

page document

{
        "_id": "6276a24e8792645dcfc6274b",
        "index": 1,
        "startingSura": 1,
        "startingAya": 1,
        "endingSura": 1,
        "endingAya": 7
}

surah document

{
        "_id": "6276a29b8792645dcfc62aa9",
        "id": 1,
        "name": "الفاتحة",
        "englishName": "Al-Fatiha",
        "ayaCount": 7,
        "type": "Meccan",
        "juz": 1
}

I am trying to get all pages that belong to a specific surah [starting sura and ending surah should match the parameter passed surahID] here's my attempt, I tried other ways but it did not work with me

async getQuranPageBySurahId(surahId) {
        return Page.aggregate([
            {
                $lookup: {
                    from : "Surah",
                    localField: '_id',
                    foreignField : "index",
                    as: 'pages'

                },


                $match: {
                    $expr: {
                        $and: [
                            {$eq: ['$startingSura', `${surahId}`]},
                            {$eq: ['$endingSura', `${surahId}`]},
                        ]
                    }
                }
            }
        ])
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source