带虚拟的Mongoose模式可在2个字段上查询

问题描述

我在NodeJS服务器应用程序中有3种不同的Mongoose模式,如下所示:

//Comment Schema
var commentSchema = mongoose.Schema({
  text: { type: String,required: 'Kindly enter the comment text' },parentId: { type: mongoose.Schema.Types.ObjectId,refPath: 'type',required: 'Provide the news ID or comment ID which this comment belongs to.' },type: { type: String,enum: Constants.commentType,required: true },},{
  collection: 'comment',timestamps: true
});
module.exports = mongoose.model('Comment',commentSchema);


//User Schema
const userSchema = mongoose.Schema({
  email: { type: String,trim: true,required: true,unique: true,lowercase: true },password: { type: String },{
    collection: 'user',timestamps: true
  });
module.exports = mongoose.model('User',userSchema);

//Comment Reaction
var commentReactionSchema = mongoose.Schema({
  commentId: { type: mongoose.Schema.Types.ObjectId,ref: 'Comment',required: 'Provide the comment ID to which this comment reaction belongs to!' },user: { type: mongoose.Schema.Types.ObjectId,ref: 'User',version: { type: Number,default: 2.0 }
},{
  collection: 'commentreaction',timestamps: true
});
module.exports = mongoose.model('CommentReaction',commentReactionSchema);

我正在使用以下代码查询评论:

Comment.find({})
.then(documents => {
res.status(200).json({
        message: 'Comments fetched successfully!',comments: documents
      });
})

如果您已经注意到,每个评论反应都具有评论ID和用户ID。
在获取注释时,我希望每个记录都具有一个类似“ userLiked”的字段,该字段根据CommentReaction判断是非。

是否可以使用虚拟和填充来实现?或者有其他选择吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...