How to search data

I am getting all data using localField and foreignField but how to seach data by name

     {
                $lookup: {
                    from: "users",
                    localField:"_id",
                    foreignField:"s_id",
                    as: "user"
                          }
      },

I am receiving search keyword in

const keyword = req.query.keyword
        ? {
            user_name: {
            $regex: req.query.keyword,
            $options: 'i',
          },
        }
        : {}

Hi @Avish_Pratap_Singh,

If you want to filter the parent collection, you can add a $match stage before the $lookup stage in your pipeline. If you want to filter the docs from the children collection users, you have to use the other format of the $lookup stage that includes a sub-pipeline that can help filter or transform the docs you bring from the children collection.

Cheers,
Maxime.

1 Like