M121 Optional Lab

Can someone explain what am I missing in the below query. I get 1259 as the answer for this Question

pipeline = [
    {
        "$match": {
            "cast": {"$elemMatch": {"$exists": True}},
            "writers": {"$elemMatch": {"$exists": True}},
            "directors": {"$elemMatch": {"$exists": True}}
        }
    },
    {
        "$addFields": {
            "writers": {
                "$map": {
                    "input": "$writers",
                    "as": "writer",
                    "in": {
                        "$arrayElemAt": [
                            {"$split": ["$$writer", " ("]},
                            0
                        ]
                    }
                }
            },
            "common": {
                "$size": {"$setIntersection": ["$cast", "$directors", "$writers"]},
            }
        }
    },
    {
        "$match": {
            "common": {"$gt": 0}
        }
    },
    {
        "$count": "count"
    }
]

You do not have access to the added fields in the same stage as the operation that creates the fields.

2 Likes