如何填充存在于更多子对象中的子文档

How to populate sub documents that exists in further sub objects

我有一个架构 storySchema,其中 chapters -> posts 不是外部文档,posts -> author 是外部文档。当我 populate({ path: "chapters", populate: { path: "posts", populate: {path: "author"}}}),我回来 'undefined'。

我不确定如何将 populate 用于非外来子文档。

const storySchema = new Schema({ chapters: [{ title: String, description: String, posts: [{ author: { type: Schema.Types.ObjectId, ref: "User" }, description: String }] }], });

models.Story.findOne({ _id: req.params.story_id }).populate({ path: "chapters", populate: { path: "posts", populate: { path: "author" } } }) .then(story => {})...

我希望有一个文档,其中填充了所需的 author 字段,却得到了未定义的文档。

尝试使用 dot notation.

访问子文件
Story.findOne({ _id: req.params.story_id })
   .populate('chapters.posts.author")
   .then(story => {})
   .catch(err => err);