Mongoose虚拟-计算另一个模型中引用的本地数组中的引用



我有两个模型注释报告

const mongoose = require('mongoose');
const CommentSchema = new mongoose.Schema(
{
content: {
type: String,
trim: true,
maxLength: 2048,
},
createdAt: {
type: Date,
default: Date.now,
},
parent: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Comment',
required: false,
},
replies: [
{
type: mongoose.Schema.Types.ObjectId,
ref: 'Comment',
},
],
isReply: {
type: Boolean,
default: false,
},
},
{ toJSON: { virtuals: true }, toObject: { virtuals: true } }
);
CommentSchema.virtual('reportCount', {
ref: 'Report',
localField: '_id',
foreignField: 'comment',
justOne: false,
count: true,
});
CommentSchema.virtual('reportReplyCount', {
ref: 'Report',
localField: 'replies',
foreignField: 'comment',
justOne: false,
count: true,
});
module.exports = mongoose.model('Comment', CommentSchema);

Comment具有字段回复,该字段回复是指向Comment模型的引用数组。用户可以报告评论,当这种情况发生时,新的"报告"文档将存储在"报告"集合中,其中包含对该评论的引用和对用户的引用。我在Comment Schema中有两个虚拟属性,reportCount(显示该评论的报告数(和reportReplyCount[显示评论回复的报告数]。现在reportCount可以完美地工作,但reportReplyCount却不能。当我创建一个评论以及对该评论的回复时,它会显示回复的数量,而不是报告的数量。我在谷歌上搜索了一下,但找不到类似的东西。

const mongoose = require('mongoose');

const ReportSchema = new mongoose.Schema({
description: {
type: String,
trim: true,
required: true,
maxLength: 100,
},
reporter: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true,
},
createdAt: {
type: Date,
default: Date.now,
},
comment: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Comment',
required: true,
},
});


module.exports = mongoose.model('Report', ReportSchema);

我不知道你到底想做什么,但我环顾四周,似乎没有任何现有的解决方案。虚拟是解决问题的一种方法,但我还没有看到在这种情况下使用它的答案。

您可以尝试创建一个名为reportReplyCount的新虚拟,它显示回复报告的数量。然后在Comment上使用aggregate,并用新的virtual替换reportCount。您可以使用以下内容:

CommentSchema.virtual('reportReplyCount', {
ref: 'Report', // reference to Report model
localField: 'replies', // matches field Comment Schema has named 'replies' 
foreignField: 'comment', // matches field Report Schema has named 'comment' (foreign key in Report model) 
justOne: false, // this is going to return all related documents, not just one (just like reportCount)  
count: true, // set it to true so it returns a number instead of an array of documents  
});
CommentSchema.methods = { ... }
CommentSchema.statics = { ... }
module.exports = mongoose.model('Comment', CommentSchema);

如果你能为你的问题找到另一个解决方案,我会避免在你的情况下使用虚拟机。

顺便说一句,我看到开发人员创建了一个新的模型作为虚拟模型,如下所示:

const mongoose = require('mongoose');
const ReportSchema = new mongoose.Schema({
description: {
type: String,
trim: true,
required: true,
maxLength: 100,
},
reporter: { // reference to User model (foreign key) 
type: mongoose.Schema.Types.ObjectId, 
ref: 'User',  // reference to User model (foreign key) 
});
module.exports = mongoose.model('Report', ReportSchema);
// Now you need an instance of that new Schema called VirtualReport. The schema must follow the same format as the "real" Report's schema did above but with a few extra parameters that refer to the virtual and it's definition (as in how it will behave). 
const VirtualReportSchema = new mongoose.Schema({ ... }, { _id : false });
module.exports = mongoose.model('VirtualReport', VirtualReportSchema);

然后你所需要做的就是,在你的模式中,有虚拟的:

// Now you can use VirtualReport like any other model. It will work just like Report but it won't get stored in the database. 
CommentSchema.virtual('reportReplyCount', {
ref: 'VirtualReport', // reference to VirtualReport model 
localField: 'replies', // matches field Comment Schema has named 'replies' 
foreignField: 'comment', // matches field VirtualReport Schema has named 'comment' (foreign key in VirtualReport model) 
justOne: false, // this is going to return all related documents, not just one (just like reportCount)  
count: true, // set it to true so it returns a number instead of an array of documents  
});
CommentSchema.methods = { ... }
CommentSchema.statics = { ... }
module.exports = mongoose.model('Comment', CommentSchema);

但请注意,虚拟机的定义("它将如何表现"(必须包含设置为false的_id属性(否则将引发错误(。这是因为当在子文档中使用虚值并且用户通过点表示法(例如,commentToBeDeleted[parent].reportReplyCount(引用它们时,点表示法试图访问虚值的_id属性。如果设置为false,点表示法将无法找到虚拟值,您将得到一个错误。所以不要忘记将_id属性设置为false!

顺便说一句,这个问题是在这里提出的。很不幸的是,这个问题是在Stack Overflow上提出的,而不是MongoDB自己的文档,其中提供了一个虚拟解释的链接(也有关于"justOne"的评论,但至少它直接引用了文档(。

最新更新