如何填充外键mongoose?

时间:2018-03-10 12:18:05

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

if (req.params) {
   const val = await userServices.find()
       .where({
           title: req.params.categoryName
       })
       .populate('profileId', null,
           {
               stateId: { $in: [`${req.params.stateId}`] },
               cityId: { $in: [`${req.params.cityId}`] },
           }
       );
   return res.json({
       success: true,
       val
   })
}

详细说明: 以下是填充个人资料详细信息的查询,但我想在StateIdCityId的外键中填充。

1 个答案:

答案 0 :(得分:0)

嗯,你实际上可以这样做:

.populate({
    path: 'profileId',
    populate: {path:'stateId'}
  })
.populate({
    path: 'profileId',
    populate: { path: 'cityId'}
});

参考:Deep Populate