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

查看:97
本文介绍了猫鼬: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

I think this post will be right for you http://www.mongodb.org/display/DOCS/Schema+Design

用例

客户/订单/订单行项目

订单应为集合.客户集合.订单项应该是订单对象中嵌入的一系列订单项.

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,10gen

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

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

Indexing & Query Optimization Alvin Richards, Senior Director of Enterprise Engineering

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

**这2个视频是mongoddb上有史以来最好的imho *

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

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

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