我有一个像这样的视频架构:
const VideoSchema = new mongoose.Schema({
caption: {
type: String,
trim: true,
maxlength: 512,
required: true,
},
owner: {
type: mongoose.Schema.ObjectId,
ref: 'User',
required: true,
},
// some more fields
comments: [{
type: mongoose.Schema.ObjectId,
ref: 'Comment',
}],
commentsCount: {
type: Number,
required: true,
default: 0,
},
}, { timestamps: true });
和一个像这样的简单评论模式:
const CommentSchema = new mongoose.Schema({
text: {
type: String,
required: true,
maxLength: 512,
},
owner: {
type: mongoose.Schema.ObjectId,
ref: 'User',
required: true,
},
videoId: {
type: mongoose.Schema.ObjectId,
ref: 'Video',
required: true,
index: true,
},
}, { timestamps: true });
使用这样的模式,我可以对我的 Video 集合执行任何类型的查找查询,并用它的评论填充它:
Video.find({ owner: someUserId }).populate({ path: 'comments' });
我的问题是在视频集合中保留评论 ID 有多大必要?鉴于我已经在我的 Comment 模式中索引了 videoId 字段,那么摆脱这些评论 id 和它们的数量并使用聚合 $lookup 来查找视频的评论会有多糟糕(谈到性能),如下所示:
Video.aggregate([
{
$match: {
owner: someUserId,
},
},
{
$lookup: {
from: 'comments',
localField: '_id',
foreignField: 'videoId',
as: 'comments',
}
}
])
这些在性能方面有何不同?