【问题标题】:How to populate a field inside a embedded document in mongoose?如何在猫鼬的嵌入式文档中填充字段?
【发布时间】:2021-07-26 19:46:35
【问题描述】:

我有一个类似这样的模型“帖子”

{
   // Some other fields
   comments: [
    new mongoose.Schema({
      content: {
        type: String,
        required: [true, "Content for a comment is required."],
      },
      author: {
        type: mongoose.Types.ObjectId,
        ref: "User",
        required: [true, "Author for a comment is required."],
      },
    }),
}

在获取帖子后,我想不出一种方法来填充 comment 内的 author 字段。我该怎么做?

【问题讨论】:

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


    【解决方案1】:

    试试Populating across multiple levels

    Post.find({ }).populate({
        path: 'comments',
        populate: { path: 'author' }
    });
    

    【讨论】:

    • Ahhhhhh .. 我之前遇到过类似的解决方案。但我认为这个工作'cmets'也必须参考另一个文件。有效。谢谢。
    猜你喜欢
    • 2012-10-16
    • 2019-07-17
    • 2021-02-11
    • 2022-12-10
    • 2018-10-15
    • 2012-05-11
    • 1970-01-01
    • 2021-10-07
    相关资源
    最近更新 更多