【问题标题】:How to have selective field for deep population query in mongoose如何在猫鼬中为深度人口查询提供选择性字段
【发布时间】:2017-11-16 21:55:11
【问题描述】:

我的查询如下图:

orderSubInfo.find({
    'order_id': {
        $in: orderArray
    }
}).populate({
    path: 'order_id',
    populate: {
        path: 'foodtruck_id',
        model: 'foodtruck'
    }
}).exec().then((info) => {
    res.json({
        status: '200',
        message: 'Your OTP',
        data: info
    })
}).catch((err) => {
    res.json({
        status: '500',
        message: 'Oops,something went wromg'
    })
});

现在发生在populate {path:foodtruck_id,model:'foodtruck} 之后。我得到了不必要的字段。但我只想拥有foodtruck_name,foodtruck_location。那么我应该如何修改我的查询呢?

【问题讨论】:

    标签: mongoose mongoose-schema mongoose-populate


    【解决方案1】:

    仅选择您想要包含的字段 (mongoose population):

    populate: {
        path: 'foodtruck_id',
        model: 'foodtruck',
        select: 'foodtruck_name foodtruck_location' //to exclude _id add -_id
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-07
      • 2013-10-07
      • 1970-01-01
      • 2020-08-07
      • 1970-01-01
      • 1970-01-01
      • 2014-11-13
      相关资源
      最近更新 更多