对象的 MongoDB 关系 [英] MongoDB relationships for objects

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

问题描述

请原谅我的英语,我还在努力掌握它.

Please excuse my english, I'm still trying to master it.

我已经开始学习 MongoDB(来自 C# 背景)并且我喜欢什么是 MongoDB 的想法.我对互联网上的示例有一些问题.

I've started to learn MongoDB (coming from a C# background) and I like the idea of what is MongoDB. I have some issues with examples on the internet.

以流行的博客文章/评论为例.帖子没有或许多与之相关的评论.我创建了 Post 对象,在 Post 的 IList 中添加了一些 Comment 对象.没关系.

Take the popular blog post / comments example. Post has none or many Comments associated with it. I create Post object, add a few Comment objects to the IList in Post. Thats fine.

我是将它添加到 MonoDB 中的帖子"集合中,还是应该有两个集合 - 一个是 blog.posts 和 blog.posts.comments?

Do I add that to just a "Posts" Collection in MonoDB or should I have two collections - one is blog.posts and blog.posts.comments?

我有一个相当复杂的对象模型,最简单的方法是把它想象成一个银行系统——我们的就是挖矿.我试图用方括号突出显示表格.

I have a fair complicated object model, easiest way to think of it is as a Banking System - ours is mining. I tried to highlight tables with square brackets.

[Users] 有一个或多个 [Accounts] 有一个或多个 [Transactions],其中一个且只有一个 >[类型].[Transactions] 可以为交易分配一个或多个 [Tag].[用户]为该用户帐户创建自己的[标签],我们有时需要根据这些标签提供报告(例如,对于 5 月,标签钻取费用为 123456.78 美元).

[Users] have one or many [Accounts] that have one or many [Transactions] which has one and only one [Type]. [Transactions] can have one or more [Tag] assigned to the transaction. [Users] create their own [Tags] unique to that user account and we sometimes need to offer reporting by those tags (Eg. for May, tag drilling-expense was $123456.78).

对于索引,我原以为将它们分开会很好,但我担心从旧的 RBDMS 时代开始这种想法是不好的做法.

For indexing, I would have thought seperating them would be good but I'm worried it is bad practice this thinking from old RBDMS days.

在某种程度上,它就像博客示例.我不确定我是否应该有 1 个 [帐户] 集合并将所有信息保留在那里,或者有一个中间步骤将其拆分为单独的集合.

In a way, its like the blog example. I'm not sure if I should have 1 [Account] Collection and persist all information there, or have an intermediate step that splits it up to seperate collections.

另一个相关的查询是,当您来回坚持时,您通常会返回与该记录相关的所有内容 - 即使不是必需的还是您有限制?

The other related query is, when you persist back and forth, do you usually get back everything associated with that record - even if not required or do you limit?

推荐答案

视情况而定.

这取决于您希望拥有的每种类型的对象有多少.您能否将它们全部放入一个给定用户的单个 MongoDB 文档中?可能不会.

It depends on how many of each of these type of objects you expect to have. Can you fit them all into a single MongoDB document for a given User? Probably not.

这取决于关系 - user-Account 是一对多关系还是多对多关系?如果是一对多且帐户数量很少,您可能会选择将它们放在用户文档的 IList 中.

It depends on the relationships - is user-Account a one-to-many or a many-to-many relationship? If it's one to many and the number of Accounts is small you might chose to put them in an IList on a User document.

您仍然可以使用单独的集合在 MongoDB 中对关系进行建模,但数据库中没有联接,因此您必须在代码中进行.从性能角度来看,加载用户然后加载他们的帐户可能没问题.

You can still model relationships in MongoDB with separate collections BUT there are no joins in the database so you have to do that in code. Loading a User and then loading their Accounts might be just fine from a performance perspective.

您可以在文档上索引 INTO 数组.不要认为索引只是文档(如 SQL)上简单字段的索引.例如,您可以在文档上使用 Tag 集合并索引到标签中.(参见 http://www.mongodb.org/display/DOCS/Indexes#Indexes-Arrays)

You can index INTO arrays on documents. Don't think of an Index as just being an index on a simple field on a document (like SQL). You can use, say, a Tag collection on a document and index into the tags. (See http://www.mongodb.org/display/DOCS/Indexes#Indexes-Arrays)

当您检索或写入数据时,您可以对任何文档进行部分读取和部分写入.(参见 http://www.mongodb.org/display/DOCS/Retrieving+a+子集+的+字段)

When you retrieve or write data you can do a partial read and a partial write of any document. (see http://www.mongodb.org/display/DOCS/Retrieving+a+Subset+of+Fields)

最后,当您看不到如何使用集合和索引获得想要的东西时,您或许可以使用 map reduce 来实现.例如,要查找当前使用的所有标签并按其使用频率排序,您可以映射每个文档中使用的标签,然后您将减少该集合得到你想要的结果.然后,您可以永久存储该 map reduce 的结果,并仅在需要时更新它.

And, finally, when you can't see how to get what you want using collections and indexes, you might be able to achieve it using map reduce. For example, to find all the tags currently in use sorted by their frequency of use you would map each document emitting the tags used in it, and then you would reduce that set to get the result you want. You might then store the result of that map reduce permanently and only up date it when you need to.

另一个问题:您提到按标签计算总数.如果您想要会计质量的事务一致性,MongoDB 可能不是您的正确选择.最终一致性"是 NoSQL 数据存储的游戏名称,它们通常不适合金融交易.例如,如果一个用户看到有 3 条评论的博客文章而另一个用户看到 4 条评论并不重要,因为他们遇到了尚未同步的不同副本,但对于财务报告,这种一致性确实很重要 - 您的报告可能不会加起来!

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

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