嵌入式文档对比参考在mongoose设计模型? [英] Embedded document vs reference in mongoose design model?

查看:107
本文介绍了嵌入式文档对比参考在mongoose设计模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我正在使用Node.js和mongoose建立一个讨论论坛。用户可以拥有多个论坛,论坛可以有多个评论。用户也可以邀请其他用户加入论坛。因此,我的问题是关于模型设计,使用参考或嵌入式文档!



如果我使用嵌入式文档,它将如下所示:

  var Comment = new Schema({...}); 

var Forum = new Schema({
title:{type:String},
content:{type:String},
comments:[Comment],
参加者:[用户]
});

var User = new Schema({
name:{type:String},
email:{type:String},
论坛:[论坛]
});

var Account = mongoose.model('Account',User);

使用上述设计,我挣扎着:当用户向论坛添加评论时,论坛在我的论坛,我不认为我可以在我的论坛列表中获得更新的新评论。我吗在这种情况下,您是否知道如何使嵌入式文档工作?



因此,我正在考虑在mongoose中使用参考。在这种情况下,我将有两个系列:帐户论坛。在这种情况下,向论坛添加新评论不是问题。我是对的吗?



这个应用程序的参考嵌入式文档要好吗?



提前感谢

解决方案

主要取决于您将如何查询和更新你的资料在这种情况下,一致性和文档大小也很重要。引用或嵌入文档时,这是一个很好的摘要:



嵌入:





  • 不定期更改的数据

  • 最终一致性可以接受


  • 您经常需要执行第二个查询来获取的数据

  • 快速阅读



参考:




  • 大型子文档

  • 数据

  • 必须立即保持一致性

  • 增长量很大的文档

  • 您经常排除的数据从结果

  • 快速写入



这是一本关于我看过的mongo的书的解释。这些只是一般规则,但根据我的经验,使用它们使得它非常清楚地引用或嵌入大部分时间。



在这种情况下,我宁愿参考论坛。但请考虑您的所有要求。例如,如果您从用户引用论坛,并且您需要查询特定论坛的所有用户,则在这种情况下查询可能较慢。如果我是你,我将撰写我需要的一切清单,然后使用一般规则将找到嵌入和引用的优缺点之间的平衡。



希望它有帮助!


Let's say I am building a discussion forum using Node.js, and mongoose. A user can have multiple forums, and a forum can have multiple comments. A user can also invite other users to join a forum too. Thus, my question is about the model design either using reference or embedded document !

If I go with embedded document, It would look like:

var Comment = new Schema({ ... });

var Forum = new Schema({
    title: {type: String},
    content: {type: String},
    comments: [Comment],
    attendees: [User]
});

var User = new Schema({
    name: {type: String},
    email: {type: String},
    forums: [Forum]
});

var Account = mongoose.model('Account', User);

Using the above design, I struggled with: when a user adds a comment to a forum, and that forum is in my forums, I don't think I would be able to get update of a new comment in my forum list. Do I ? Do you know how to get the embedded document to work in this case?

Thus, I was thinking of using reference in mongoose. In this case, I will have two collections: Account, and Forum. Adding a new comment to a forum is not a problem in this case. Am I right?

Would reference be better than embedded document for this app?

Thanks in advance,

解决方案

It depends mostly on how you're gonna query and update your data. Consistency and document size is also important in this case. Here's a good summary on when referencing or embedding documents:

Embedding:

  • Small subdocuments
  • Data that does not change regularly
  • Eventual consistency is acceptable
  • Document that grow by a small amout
  • Data that you will often need to perform a second query to fetch
  • Fast reads

Referencing:

  • Large subdocuments
  • Volatile data
  • Immediate consistency is necessary
  • Document that grow a large amount
  • Data that you will often exclude from results
  • Fast writes

This is an exctract from a book on mongo I read. These are just general rules but from my experience, using them makes it very clear wether to reference or embed most of the times.

I would rather reference Forum in this case. But please consider all your requirements. For example if you reference Forum from User and you need to query all User of a particular Forum the query might be slow in this case. If I were you I would compose a list of everything I need and then using general rules would find a balance between pros and cons of embeding and referencing.

Hope it helps!

这篇关于嵌入式文档对比参考在mongoose设计模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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