mongoose如何填充模型中引用的数组
我正在制作一个博客应用程序。两个模型comment和userProfile,comment如下所示:mongoose如何填充模型中引用的数组,mongoose,Mongoose,我正在制作一个博客应用程序。两个模型comment和userProfile,comment如下所示: var commentSchema=新模式({ userId:{type:Schema.Types.ObjectId,ref:'userProfile'}, replyCommentId:[{type:Schema.Types.ObjectId,ref:'Comment'}], commentDesc:String, 日期:{type:String,默认值:date.now()} }); 我已
var commentSchema=新模式({
userId:{type:Schema.Types.ObjectId,ref:'userProfile'},
replyCommentId:[{type:Schema.Types.ObjectId,ref:'Comment'}],
commentDesc:String,
日期:{type:String,默认值:date.now()}
});
我已将回复保存为注释本身,并通过存储在数组中的replycomment id引用主注释
我试图填充那些回复字段
commentsModel.findOne({u-id:commentId}).populate('replyCommentId'))
.populate('replyCommentId.userProfile').exec(函数(err,docs){
cb(错误、文件);
})
我可以用model填充
replyCommentId
,但是replyCommentId
中的userProfile
没有填充 您可以这样尝试:
。。。
常量populateQuery=[];
populateQuery.push({
路径:“replyCommentId”
}, {
路径:“replyCommentId”,
填充:{
路径:“用户配置文件”,
模型:“注释”,
},
});
fields.commentsModel.findById(commentId)
.populate(populateQuery)
.exec((错误,文档)=>{
cb(错误、文件);
});
..
您不需要使用两个填充任务来填充其中的replyCommentId和userId。它可以使用一个填充来完成
您可以简单地尝试以下方法:
commentsModel
.findOne({_id: commentId}).
.populate({
path : 'replyCommentId',
populate : {
path :'userId'
}
}).exec(function (err, docs) {
cb(err, docs);
});
有关猫鼬填充及其选项的更多信息,请参阅。查找跨多个级别填充的我已经尝试过了,它不起作用。用户ID没有使用模型填充,它仍在被引用。您可以尝试我编辑的答案。这对你有用,对我有用。我明白你的逻辑。非常感谢。我可以给你投票,但我是新来的堆栈溢出,我正在建立我的声誉。如果你接受我的回答,你会得到2个声誉;)