mongodb汇总$ lookup vs查找并填充 [英] mongodb aggregate $lookup vs find and populate

查看:48
本文介绍了mongodb汇总$ lookup vs查找并填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的视频模式:

I have a Video Schema like this:

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 });

和类似这样的简单注释模式:

and a simple Comment schema like this:

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 });

并使用这样的模式,我可以对我的视频收藏集执行任何类型的查找查询,并在其中添加评论:

and with schemas like this I'm able to perform any kind of find query on my Video collection and populate it with its comments:

Video.find({ owner: someUserId }).populate({ path: 'comments' });

我的问题是,在视频收藏夹中保留评论ID的必要性是什么?鉴于我已在Comment模式中为videoId字段建立了索引,摆脱这些注释ID及其计数并使用聚合$ lookup来查找视频的注释将是多么糟糕(就性能而言):

My question is how necessary is it to keep comment ids inside video collection? given that I have indexed the videoId field in my Comment schema, how bad it would be (speaking of performance) to get rid of these comment ids and the count of them and use aggregation $lookup to find a video's comments like this:

Video.aggregate([
  {
    $match: {
      owner: someUserId,
    },
  },
  {
    $lookup: {
      from: 'comments',
      localField: '_id',
      foreignField: 'videoId',
      as: 'comments',
    }
  }
])

这些在性能方面有何不同?

How different are these in terms of performance?

推荐答案

好吧, $ lookup 不会比在实际视频对象上具有注释ID的列表更快.我的意思是,您必须对mongo进行其他请求才能立即获得它们.因此,从性能角度来看,显然查找会增加时间.假设您没有使用 mongoose populate 将这些注释ID转换"为引用的对象.

Well there is no way the $lookup would be faster than having the list of the comment ids on the actual video object. I mean you have to do a whole other request to mongo to get them now. So performance wise obviously the lookup would add time. That is assuming you are not using mongoose populate to "convert" those comment ids into the referenced objects.

如果您要从视频中删除评论(以及实际计数道具),然后执行查找.由于您要立即在arg中进行匹配,然后执行简单的 lookup ,所以我看不出这对您来说是一个瓶颈.另外,您还可以优化/更改/调整聚合竞争解释

If you are then removing the comments from the video (as well as the actual count prop) and doing the lookup is the way to go. Since you are matching right away in your arg and then doing a simple lookup I do not see how this would be a bottleneck for you. Also you can optimize/change/tune your aggregation vie explain etc.

您的视频架构将非常干净:

You video schema would be pretty clean that way:

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
}, { timestamps: true });

这篇关于mongodb汇总$ lookup vs查找并填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆