【问题标题】:In mongoose query, how can I apply query on a virtual data I got from virtual schema using .populate method?在猫鼬查询中,如何使用 .populate 方法对从虚拟模式获得的虚拟数据应用查询?
【发布时间】:2022-02-03 15:40:09
【问题描述】:
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[];

在此,我如何查询从 request_to_user_information 获得的数据作为虚拟数据?

【问题讨论】:

    标签: node.js mongodb mongoose mongodb-query mongoose-populate


    【解决方案1】:

    所以,在尝试了一段时间后,我得到了解决这个问题的方法,即,

        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.",
                 });
             }
    

    【讨论】:

    • 欢迎来到 StackOverflow!如果您能简要解释一下此解决方案为何有效,那可能会很好。
    猜你喜欢
    • 1970-01-01
    • 2021-08-17
    • 2013-09-13
    • 2013-08-24
    • 2016-09-13
    • 2017-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多