'In mongoose query, how can I apply query on a virtual data I got from virtual schema using .populate method?

const query = {
  from_user_id: userId,
};

const data = await Contact.find(query).populate({
  path: 'request_to_user_information',
  select:
    'first_name last_name profile_image country city zip designation company_name',
  populate: [
    { path: 'country', modal: 'country' },
    { path: 'city', modal: 'city' },
  ],
}) as IContact[];

In this, how can I query on data I am getting from request_to_user_information as a virtual data?



Solution 1:[1]

So, after trying this for some time I got the solution for this problem and i.e.,

    if (userId) {
             const query = {
                 from_user_id: userId,
             };
             const data = await Contact.find(query).populate({
                 path: "request_to_user_information",
                 select: "first_name last_name profile_image country city zip designation company_name",
                 populate: [{ path: "country", modal: "country" }, { path: "city", modal: "city" }],
             }) as IContact[];
             res.send({
                 success: true,
                 message: "data fetched successfully.",
                 data
             });
         }
         else {
             res.status(404).send({
                 success: false,
                 message: "Something went wrong.",
             });
         }

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Ankit Dhoot