简体   繁体   English

设计具有多对多关系的mongodb模式

[英]Designing a mongodb schema featuring a many-to-many relation

Hi I've begun learning Mongo on a small home project to determine if its viable as a solution for one of our products at work. 嗨,我已经开始在一个小型家庭项目中学习Mongo,以确定它是否可以作为我们工作中的一种产品的解决方案。 Before heading down the path I've dreamed up I wanted to run this by stack overflow to determine if this structure is sane and that data can be efficiently accessed n years from now. 在我梦I以求的道路之前,我想通过堆栈溢出来运行此命令,以确定此结构是否合理,并且可以从现在开始有效地访问数据。

The application is one that collects spreads for football games and allows users to place mock bets. 该应用程序可以收集足球比赛的点差,并允许用户进行模拟投注。 This opens the door for all sorts of interesting analytics down the line from betting patterns to how spreads change over time etc. 这为从下注模式到点差如何随时间变化等各种有趣的分析打开了大门。

  • users 用户
    • bets[] 赌注[]
  • teams 球队
    • standings[] 榜[]
  • games 游戏
    • away team{} 客队{}
    • home team{} 主队{}
    • bets[] 赌注[]

You'll notice from the list above I have bets listed twice. 您会从上面的列表中注意到,我有两次下注。 This is one of the big questions I have. 这是我的主要问题之一。 Where would a doc that stores the game a user bet on be stored? 将用户下注的存储游戏的文档存储在哪里? In the games or in the users collection. 在游戏或用户集合中。 Both make sense really. 两者都确实有意义。 If I store it one vs the other than I have to query a big list in the other for showing the data. 如果我将它与另一个存储,则必须在另一个中查询一个大列表以显示数据。 For instance if I have users.bets then I when I want to show all the bets for a given game I have to look for that game.id in all of users.bets and vice-versa. 例如,如果我有users.bets,那么当我想显示给定游戏的所有投注时,我必须在所有users.bets中寻找该game.id,反之亦然。 Is this efficient? 这样有效吗? Sane? 理智?

I guess this is a struggling point for me. 我想这对我来说是一个挣扎点。 I know how to deal with many-to-many in a relational database, but not here. 我知道如何处理关系数据库中的多对多关系,但这里不行。 Is a 3rd collection for storing the many-to-many still the way to go in mongo. 是第三个集合,用于在mongo中存储多对多的行进方式。

Opinions and reference material are welcomed. 欢迎发表意见和参考资料。

There is nothing wrong with denormalizing data - if it gets queried in multiple places, then in your application it makes sense to store it in multiple places. 对数据进行非规范化没什么错-如果在多个位置查询数据,则在应用程序中将其存储在多个位置是有意义的。

Things to consider when you are thinking how to structure things is what the data will look like in X months when there is a lot more of it. 当您考虑如何构造事物时要考虑的事情是X个月中数据量更多时的样子。

If this application collects data for many years, it might be problematic to have all the bet history embedded in the user document - will you need all of their history every time you load their document? 如果此应用程序收集了很多年的数据,则将所有投注历史记录嵌入到用户文档中可能会出现问题-每次加载其文档时,您是否需要它们的所有历史记录? Or will most of it be useless and not relevant? 还是大多数将是无用的且不相关的?

I would encourage you to consider storing data you need "together" together, but be realistic and critical about whether you will always need all of that data. 我鼓励您考虑将您需要的数据“一起”存储在一起,但是对于您是否始终需要所有这些数据要现实且重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM