仅填充猫鼬中的特定字段 [英] Populating only specific fields in mongoose

查看:40
本文介绍了仅填充猫鼬中的特定字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的文档:

信息收集:

{
  infoType: "Appointment",
  contact: ObjectId("5baa28a3f1268917e9220138"),
}

联系人收集:

{
  name: "ABC",
  email: "abc@def.com"
}

我想通过在 mongoose 中使用 populate 方法只填充查询中的联系人姓名.

And I want to populate only contact name in query by using populate method in mongoose.

推荐答案

试试这个它可能会帮助你得到你想要的.

Try this it will might help you to get exactly what you want.

infoModel.find({contactId: ObjectId("5baa28a3f1268917e9220138")})
.populate('contact', "name email")
.then(infoDetails => {
    console.log("Info Details -> ", infoDetails)
}).catch(err => {
    console.log("Error Occured -> ", err)
})

请参阅文档以获取更多知识.

Refer document for further knowledge.

这篇关于仅填充猫鼬中的特定字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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