猫鼬种群中是否可能有条件 [英] Is it possible to have condition in mongoose populate

查看:51
本文介绍了猫鼬种群中是否可能有条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在包含 serviceIDtype 的集合中有一个名为 services 的子文档.serviceID 是指两个集合 internalserviceexternalservice 根据类型字段而变化.

I have a sub document called services in a collection it contains serviceID and type. serviceID is referred to two collections internalservice and externalservice varies based on the type field.

internalServices:[{
           serviceID:ObjectId('0000000000000'),
           serviceDesc:'sweeping'
         },
         {
           serviceID:ObjectId('0000000000000'),
           serviceDesc:'floor cleaning'
         }]


 externalServices:[{
               serviceID:ObjectId('0000000000000'),
               serviceDesc:'painting'
             },
             {
               serviceID:ObjectId('0000000000000'),
               serviceDesc:'white washing'
             }]

以上两个是查找集合.这里是公司合集

Above two are lookup collection. And here the company collection

   _id:"xxxxxxxxxxxxxxxx",
   name: 'xxxx',
   Address:'xxx',

    services:[{
           serviceID:ObjectId('0000000000000'),
           type: internalservice
         },
         {
           serviceID:ObjectId('0000000000000'),
           type: externalservice
         }]

在这里,我想根据类型填充服务描述.

Here, i want to populate service description based on type.

是否可以在填充时设置条件路径?

Is it possible to have conditional path at populate?

这是我的代码

Company.findOne({"_id": mongoose.Types.ObjectId(req.params.companyID)},
                {})
             .populate('services.serviceID','serviceDesc','InternalService')
             .exec(function (err, companyInfo) {

               console.log(companyInfo);
             });

这会填充内部服务描述,但我需要为 externalservice 类型填充外部服务描述.当我有两个填充时,第二个填充将用 null 替换不匹配文档的结果.

This populates internal service description but i need to populate external service description for type externalservice. When i have two populate the second populate replaces the result with null for unmatched documents.

推荐答案

您可以在填充 where 条件中包括:

You can include in populate where conditions like:

Story
.find(...)
.populate({
  path: 'fans',
  match: { age: { $gte: 21 }},
  select: 'name -_id',
  options: { limit: 5 }
})
.exec()

这里是猫鼬种群文档

这篇关于猫鼬种群中是否可能有条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆