Node.js 如何在mongoose中的嵌入式文档中填充字段?
我有一个类似这样的模型“Post”Node.js 如何在mongoose中的嵌入式文档中填充字段?,node.js,mongodb,express,mongoose,mongodb-query,Node.js,Mongodb,Express,Mongoose,Mongodb Query,我有一个类似这样的模型“Post” { // Some other fields comments: [ new mongoose.Schema({ content: { type: String, required: [true, "Content for a comment is required."], }, author: { type: mongoose.Type
{
// 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
字段的方法。我该怎么做呢?试试看
啊。。我之前也遇到过类似的解决方案。但我认为,要使这一点起作用,“评论”还必须参考另一份文件。成功了。谢谢你。@sammanadhikari很高兴它成功了:)
Post.find({ }).populate({
path: 'comments',
populate: { path: 'author' }
});