$lookup from array of object and match a field in different collection

I have my schema like this:

carsCollection: {
     _id: "cars-01"
    booked: [
        {
            user_id: "user-01",
            time: "time-01"
        },
        {
            user_id: "user-02",
            time: "time-02"
        }
    ]
}

Another one:

usersCollection: {
    _id: "user-01",
    avatar: "avatarLink",
}

So, How can I first match carsCollection by “_id” and then find all the user profile who booked it?

What have you tried so far?

What issues did you get?

Matching is simple see https://docs.mongodb.com/manual/reference/operator/aggregation/match/. You will find some example.

To $lookup you have to $unwind the array booked first. See https://docs.mongodb.com/manual/reference/operator/aggregation/unwind/ for some example.

As for the $lookup itself, the simple case https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/#equality-match-with-a-single-join-condition should be good with your localField being user_id and foreingField being _id.

So 3 simple stages [ match , unwind , lookup ].

I tried,

cars.aggregate([
    {
        $match: { _id: "cars-01" }
    },
    {
        $lookup: {
            from: "users",
            let: { bookedUser: "$booked" },
            pipeline: [
                {
                    $match: {
                        $expr: {
                            $and: [{ $eq: ["$$bookedUser.user_id", "$_id"] }]
                        }
                    }
                }
            ],
            as: "BookedUser"
        }
    }
])

But it didn’t work.

As mentioned

So between your $match and $lookup you need to $unwind so that your pipeline is

As for the $lookup, like mentioned