Mongo DB 设计,嵌入与关系 [英] Mongo DB Design, embedding vs relationships

查看:14
本文介绍了Mongo DB 设计,嵌入与关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个简单的会计系统,其中用户有很多账单.现在我正在尝试决定 bills 是否应该是它自己的集合,或者嵌套在用户中.我倾向于前者,但我从来没有做过任何 noSQL 的东西,所以我只是通过反复试验和我认为对我来说有意义的事情.

I'm building a simple accounting system where a user has many bills. Now I'm trying to decide if bills should be its own collection, or nested within the user. I'm leaning towards the former but I've NEVER done any noSQL stuff so I'm just going by trial and error and what I think makes sense to me.

我知道 Mongo 有 4mb 的文档大小限制,这让我认为我应该单独收集账单,因为这些账单会每天累积,最终可能会占用大量空间.

I understand that Mongo has a 4mb document size limit which is what's making me think that I should have a separate collection for bills, as these will accumulate daily and could eventually take up a large amount of space.

我只是在寻找对此事的意见.基本上,我将查询不同日期期间的用户账单(您可以想象会计系统会这样做).

I'm just looking for opinions on the matter. Basically I'll be querying for bills of a user between different date periods (as you can imagine an accounting system would do).

这并不重要,但我在 Rails3 项目中使用 Mongoid.我想我会做类似的事情:

Not that it really matters but I'm using Mongoid in a Rails3 project. I figured I'd do something like:

class User
  references_many :bills
end

class Bill
  referenced_in :user
end

非常感谢任何意见或设计建议.

Any comments or design suggestions are greatly appreciated.

推荐答案

1) 关于 4MB 文档的限制,《MongoDB:权威指南》是这么说的:

1) Regarding the 4MB document limit, this is what the "MongoDB: The Definitive Guide" says :

大于 4MB 的文档(转换为 BSON 时)无法保存到数据库中.这是一个有点武断的限制(将来可能会提高);它主要是为了防止糟糕的架构设计并确保一致的性能.要查看文档 doc 的 BSON 大小(以字节为单位),请从 shell 运行 Object.bsonsize(doc).

Documents larger than 4MB (when converted to BSON) cannot be saved to the database. This is a somewhat arbitrary limit (and may be raised in the future); it is mostly to prevent bad schema design and ensure consistent performance. To see the BSON size (in bytes) of the document doc, run Object.bsonsize(doc) from the shell.

为了让您知道 4MB 是多少,War and Peace 的整个文本只有 3.14MB.

To give you an idea of how much 4MB is, the entire text of War and Peace is just 3.14MB.

最终,这取决于您预计用户的账单会增长到多大.我希望上面的摘录能让您了解文档大小所施加的限制.

In the end it depends on how big you expect the bills for a user to grow. I hope the excerpt above gives you an idea of the limits imposed by the document size.

2) 如果您知道您永远不会对账单运行全局查询(此类查询的示例是如果您想检索十最近的账单进入系统).如果您使用非规范化模式,则必须使用 map-reduce 来检索此类查询的结果.

2) De-normalized schema (bills go with the user document) is the way to go if you know that you are never going to run global queries on bills (example of such a query is if you want to retrieve the ten most recent bills entered into the system). You will have to use map-reduce to retrieve results for such queries if you use a denormalized schema.

如果您希望灵活地查询账单的方式,规范化架构(用户和账单在单独的文档中)是更好的选择.但是,由于 MongoDB 不支持联接,因此每次要检索与用户对应的账单时,都必须运行多个查询.

Normalized schema (user and bills in separate documents) is a better choice if you want flexibility in how the bills are queried. However, since MongoDB doesn't support joins, you will have to run multiple queries every time you want to retrieve the bills corresponding to a user.

鉴于您提到的用例,我会使用非规范化架构.

Given the use-case you mentioned, I'd go with de-normalized schema.

3) MongoDB 中的所有更新都是原子的和序列化的.这应该能解决史蒂夫的担忧.

3) All updates in MongoDB are atomic and serialized. That should answer Steve's concern.

您可能会发现这些幻灯片很有帮助.http://www.slideshare.net/kbanker/mongodb-meetup

You may find these slides helpful. http://www.slideshare.net/kbanker/mongodb-meetup

您还可以查看 MongoDB 的生产部署页面.您可能会发现 SF.net 幻灯片很有帮助.

You may also look at MongoDB's Production Deployments page. You may find the SF.net slides helpful.

这篇关于Mongo DB 设计,嵌入与关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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