何时在Mongo DB中嵌入文档 [英] When to embed documents in Mongo DB

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

问题描述

我试图弄清楚如何最好地设计Mongo DB模式. Mongo DB文档建议强烈依赖嵌入式文档来改进查询,但是我想知道我的用例是否确实证明了引用文档的合理性.

I'm trying to figure out how to best design Mongo DB schemas. The Mongo DB documentation recommends relying heavily on embedded documents for improved querying, but I'm wondering if my use case actually justifies referenced documents.

我当前架构的一个非常基本的版本基本上是: (对于psuedo格式的道歉,我不确定如何表达Mongo模式)

A very basic version of my current schema is basically: (Apologies for the psuedo-format, I'm not sure how to express Mongo schemas)

users {
  email (string)
}

games {
  user (reference user document)
  date_started (timestamp)
  date_finished (timestamp)
  mode (string)
  score: {
    total_points (integer)
    time_elapsed (integer)
  }
}

游戏很短(大约60秒长),我预计会发生大量并发写入.

Games are short (about 60 seconds long) and I expect a lot of concurrent writes to be taking place.

在某个时候,我将要计算一个高分列表,并可能以一种分离的方式(例如,特定游戏,模式或日期的高分列表)

At some point, I'm going to want to calculate a high score list, and possibly in a segregated fashion (e.g., high score list for a particular game.mode or date)

这里嵌入文档是最好的方法吗?还是这确实是关系可以更好地解决的问题?在Mongo DB中如何最好地解决这些用例?

Is embedded documents the best approach here? Or is this truly a problem that relations solves better? How would these use cases best be solved in Mongo DB?

推荐答案

...这真的是关系可以更好地解决的问题吗?

... is this truly a problem that relations solves better?

这里的关键不是关于这是一种关系吗?" ,而是关于我将如何访问它?"

The key here is less about "is this a relation?" and more about "how am I going to access this?"

MongoDB不是反引用". MongoDB确实 不是 具有联接的优点,但 却具有嵌入式文档的优点.

MongoDB is not "anti-reference". MongoDB does not have the benefits of joins, but it does have the benefit of embedded documents.

只要您了解这些折衷,那么在MongoDB中使用引用就很公平了.这实际上与您计划如何查询这些对象有关.

As long as you understand these trade-offs then it's perfectly fair to use references in MongoDB. It's really about how you plan to query these objects.

这里嵌入文档是最好的方法吗?

Is embedded documents the best approach here?

也许.一些需要考虑的事情.

Maybe. Some things to consider.

  • games是否具有user上下文之外的值?
  • 一个user将有几个games?
  • games本质上是事务性的吗?
  • 您将如何访问games?您是否总是需要用户的所有游戏?
  • Do games have value outside of the context of the user?
  • How many games will a single user have?
  • Is games transactional in nature?
  • How are you going to access games? Do you always need all of a user's games?

如果您打算构建排行榜,并且用户可以生成数百个游戏文档,那么将游戏放在自己的收藏中可能是公平的.在每个用户内部存储一万个游戏"实例并不是特别有用.

If you're planning to build leaderboards and a user can generate hundreds of game documents, then it's probably fair to have games in their own collection. Storing ten thousand instances of "game" inside of each users isn't particularly useful.

但是根据您对上述内容的回答,您实际上可以选择其中任何一种方式.作为石蕊测试,我将尝试运行一些Map/Reduce作业(即构建一个简单的排行榜),以了解您对数据结构的感觉.

But depending on your answers to the above, you could really go either way. As the litmus test, I would try running some Map / Reduce jobs (i.e. build a simple leaderboard) to see how you feel about the structure of your data.

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

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