Mongoose:populate()/DBref 还是数据重复? [英] Mongoose: populate() / DBref or data duplication?

查看:12
本文介绍了Mongoose:populate()/DBref 还是数据重复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个收藏:

  1. 用户
  2. 上传


每个上传都有一个与之关联的 User,当查看 Upload 时我需要知道他们的详细信息.最佳做法是在上传记录中复制此数据,还是使用 populate()_id 引用的用户集合?


Each upload has a User associated with it and I need to know their details when an Upload is viewed. Is it best practice to duplicate this data inside the the Uploads record, or use populate() to pull in these details from the Users collection referenced by _id?


选项 1

var UploadSchema = new Schema({
    _id: { type: Schema.ObjectId },
    _user: { type: Schema.ObjectId, ref: 'users'},
    title: { type: String },
});


选项 2

var UploadSchema = new Schema({
    _id: { type: Schema.ObjectId },
    user: { 
           name: { type: String },
           email: { type: String },
           avatar: { type: String },
           //...etc
          },
    title: { type: String },
});


使用选项 2",如果 Users 集合中的任何数据发生更改,我将不得不在所有关联的 Upload 记录中更新它.另一方面,使用选项 1",我可以放松一下,让 populate() 确保始终显示最新的用户数据.


With 'Option 2' if any of the data in the Users collection changes I will have to update this across all associated Upload records. With 'Option 1' on the other hand I can just chill out and let populate() ensure the latest User data is always shown.

使用 populate() 的开销很大吗?在这种常见情况下,最佳做法是什么?

Is the overhead of using populate() significant? What is the best practice in this common scenario?

推荐答案

如果您需要查询您的用户,请让用户独处.如果您需要查询上传的内容,请单独保留上传内容.

If You need to query on your Users, keep users alone. If You need to query on your uploads, keep uploads alone.

您应该问自己的另一个问题是:每次我需要这些数据时,我是否需要嵌入对象(反之亦然)?该数据将更新多少次?这些数据将被读取多少次?

Another question you should ask yourself is: Every time i need this data, do I need the embedded objects (and vice-versa)? How many time this data will be updated? How many times this data will be read?

考虑交友请求:每次您需要请求时,您都需要发出请求的用户,然后将请求嵌入到用户文档中.

Think about a friendship request: Each time you need the request you need the user which made the request, then embed the request inside the user document.

您也可以在嵌入对象上创建索引,并且您的搜索将是单查询/快速/一致的.

You will be able to create an index on the embedded object too, and your search will be mono query / fast / consistent.

只是我之前对类似问题的回复的链接:对象之间的Mongo DB关系

我认为这篇文章适合你http://www.mongodb.org/display/DOCS/Schema+Design

用例

客户/订单/订单行项目

订单应该是一个集合.客户收藏.line-items 应该是嵌入在 order 对象中的 line-items 数组.

Orders should be a collection. customers a collection. line-items should be an array of line-items embedded in the order object.

博客系统.

帖子应该是一个集合.帖子作者可能是一个单独的集合,或者只是帖子中的一个字段,如果只是一个电子邮件地址.评论应嵌入帖子中以提高性能.

Posts should be a collection. post author might be a separate collection, or simply a field within posts if only an email address. comments should be embedded objects within a post for performance.

架构设计基础

Kyle Banker,10 代

http://www.10gen.com/presentation/mongosf2011/schemabasics

索引和查询优化Alvin Richards,企业工程高级总监

http://www.10gen.com/presentation/mongosf-2011/mongodb-indexing-query-optimization

**这两个视频是 mongoddb 上见过的最好的 imho*

**These 2 videos are the bests on mongoddb ever seen imho*

这篇关于Mongoose:populate()/DBref 还是数据重复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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