Find user is registered to a Event in MongoDb (aggregation)

I tried to find users who are registered for that event.

So I join multiple collections shown below –

Events.aggregate([
            { $match: { category: "group_event" } },


            // collection where events are scheduled
            {
                $lookup: {
                    from: "group_events",
                    let: { eventId: "$eventID" },
                    pipeline: [
                        {
                            $match: {
                                $expr: {
                                    $and: [
                                        { $eq: ["$_id", "$$eventId"] },

                                        { $gt: ["$time", new Date()] },
                                    ],
                                },
                            },
                        },

                         // register user collection
                        {
                            $lookup: {
                                from: "register_events",
                                let: { eventId: "$_id" },
                                pipeline: [
                                    { $match: { $expr: { $eq: ["$eventId", "$$eventId"] } } },
                                ],
                                as: "registerUsers",
                            },
                        },

                    ],
                    as: "events",
                },
            },
            { $unwind: "$events" },
            
        ])

and the output is now comingout –

[
        {
            "_id": "614d6dfd82cb36be231083c9",
            "trainerId": "61488dc36b7ccedbc884d20a",
            "category": "group_event",
            "eventID": "614d6dfc82cb36be231083c7",
            "createdAt": "2021-09-24T06:19:41.268Z",
            "updatedAt": "2021-09-24T06:19:41.268Z",
            "__v": 0,
            "events": {
                "_id": "614d6dfc82cb36be231083c7",
                "groupName": "group name 4",
                "category": "sdfsdf",
                "time": "2021-09-27T07:44:58.762Z",
                "description": "description",
                "day": "sunday",
                "platform": "zoom",
                "notes": "22",
                "skills_to_learn": [
                    "demo"
                ],
                "status": "pending",
                "trainerId": "61488dc36b7ccedbc884d20a",
                "meetingLink": "https://us05web.zoom.us/j/81660534858?pwd=cGZaODVjdWJUQWNtN243MlNiVUN0UT09",
                "type": "group_event",
                 **isUserRegisted : true / false,**
                "createdAt": "2021-09-24T06:19:41.000Z",
                "updatedAt": "2021-09-24T06:19:41.000Z",
                "__v": 0,
                "registerUsers": [
                    {
                        "_id": "614ed6b4b8a545acb8517e85",
                        "userId": "614d59371d11becb8e23f536",
                        "eventId": "614d6dfc82cb36be231083c7",
                        "question": "",
                        "createdAt": "2021-09-25T07:58:44.939Z",
                        "updatedAt": "2021-09-25T07:58:44.939Z",
                        "__v": 0
                    }
                ]
            }
        }
    ]

which is ok for me bu just wanted to add a key: value, heighlited on obove section

isUserRegisted : true / false

i tried with $addFields but can’t came up with any solution. Basically I need to retrieve arrays from registerUsers – collection and on the same time match the userId

Answer

I was able to figure out this issue.

simply I need to use $project and $filter to get the data if available and at last use $cond to return true or false

{
    $project: {
        root: "$$ROOT",
        userFound: {
            $filter: {
                input: "$registerUsers",
                as: "ac",
                cond: {
                    $eq: ["$$ac.userId", mongoose.Types.ObjectId(userId)],
                },
            },
        },
    },
},

{
    $project: {
        _id: 0,
        document: "$$ROOT",
        userFound: {
            $cond: {
                if: { $isArray: "$userFound" },
                then: {
                    $cond: {
                        if: {
                            $gt: [{ $size: "$userFound" }, 0],
                        },
                        then: true,
                        else: false,
                    },
                },
                else: false,
            },
        },
    },
},

// merging nested object with parents
{
    $replaceRoot: {
        newRoot: {
            $mergeObjects: [
                "$document.root",
                { isUserRegistered: "$userFound" },
            ],
        },
    },
},