简体   繁体   English

MongoDB:建模喜欢,共同的朋友等

[英]MongoDB: Modeling likes, mutual friends etc

I'm building a streaming music service that will have features like: 我正在构建一种流音乐服务,该服务具有以下功能:

  • Follow users 关注用户
  • Like songs 喜欢歌曲
  • Create and manage playlists 创建和管理播放列表
  • ...etc. ...等等。

I'm using Rails 3.1, Mongoid and MongoDB. 我正在使用Rails 3.1,Mongoid和MongoDB。 I'm unsure how I should model my User, Playlist and Song models. 我不确定如何建模用户,播放列表和歌曲模型。

There is a many to many relationship between playlists and songs. 播放列表和歌曲之间存在多对多关系。 Mongoid is storing ObjectId's of each song in an array of each playlist. Mongoid将每个歌曲的ObjectId存储在每个播放列表的数组中。 But I need to add some metadata to each song <-> playlist relationship, like the position of the song in the specific playlist. 但是我需要为每首歌曲<->播放列表关系添加一些元数据,例如歌曲在特定播放列表中的位置。

I'm also unsure of how to do things like finding out which mutual friends and mutual likes a user has with another user. 我也不确定如何进行操作,例如找出某个用户与另一个用户有哪些共同的朋友和共同点。 Is is really a good idea to store a users likes and friends inside the user document? 在用户文档中存储用户的喜欢和朋友真的是一个好主意吗? A user could possibly have several thousand likes and over a thousand friends etc. 用户可能有数千个喜欢和一千多个朋友等。

Is this kind of stuff better suited for a relational database? 这种东西更适合关系数据库吗?

I know this question is old, but its worth having answers on here that other people can read. 我知道这个问题很旧,但是值得在这里得到其他人可以阅读的答案。

Mongo was built for concepts such as likes. Mongo是为喜欢之类的概念而构建的。 The document store makes is very easy to add features such as this without having to ditch your model, data or do any migration. 文档存储使添加这样的功能变得非常容易,而无需删除模型,数据或进行任何迁移。

NoSQL dbs (and mongo is not the fastest) are lightening quick so you can take advantage of this and make your model simple. NoSQL数据库(而且mongo并不是最快的)迅速减轻了负担,因此您可以利用它并使模型简单。

I would create documents called Songs, Users, Likes & Playlists, and then create associations between them. 我将创建名为“歌曲,用户,喜欢和播放列表”的文档,然后在它们之间创建关联。 You can change the logic to suit your case, but if you assume that a Playlist is shared between users but owned by one user you would create the following associations (depending on the mongo driver the syntax will be different, below is mongomapper): 您可以更改逻辑以适合您的情况,但是如果您假设一个播放列表在用户之间共享但由一个用户拥有,则您将创建以下关联(取决于mongo驱动程序,语法会有所不同,以下是mongomapper):

 Playlists: 

 many :users, :as => :shared_with_users
 one :users, :as => :owner

 Users:

 many Playlists

This will create associations between the objects. 这将在对象之间创建关联。 Under your User object you will be able to find the playlists associated by that user using user.playlist which will return a cursor of the playlist objects associated. 在您的用户对象下,您将能够使用user.playlist查找该用户关联的播放列表,这将返回关联的播放列表对象的光标。 The same as if you are using the playlist object you can look at the owners or the shared_with_users to find the objects you want that way. 与使用播放列表对象一样,您可以查看所有者或shared_with_users来查找所需的对象。

The question you should ask yourself in the first place is : Why do I want to use mongoDB for this application ? 首先,您应该问自己的问题是:为什么要为此应用程序使用mongoDB?

Your need seems well-fitted for relationnal database world (= complex relationship between objects). 您的需求似乎非常适合关系数据库世界(=对象之间的复杂关系)。 So maybe you will be happier with a relationnal database. 因此,也许您会更喜欢关系数据库。

There are many options to your problem (each of them have trade-offs of course) : 您的问题有很多选择(当然,每个选择都需要权衡):

  • you can store you M2M relation in a separate collection and do some client-side joint 您可以将M2M关系存储在单独的集合中,并进行一些客户端联合
  • you can embed songs into playlist, or playlist into songs 您可以将歌曲嵌入播放列表,也可以将播放列表嵌入歌曲
  • you can store some of your playlist (the most used for instance) into the songs and client-side join with another collection for the least used playlist 您可以将一些播放列表(例如,使用最多的列表)存储到歌曲中,并在客户端与另一个集合一起存储使用最少的播放列表
  • you can also store some information regarding playlist in your songs and the rest in an independant collection. 您还可以将有关播放列表的一些信息存储在歌曲中,其余信息存储在独立的收藏集中。
  • ... ...

Your options are quite unlimited, you have to precise your needs to be able to make a good choice. 您的选择是无限的,您必须精确地选择自己的需求才能做出不错的选择。

If you are just building you application, start with somehting very simple (maybe client side joint is easy even if it requires more requests) and then start improving your model depending what your bottlenecks are. 如果您只是在构建应用程序,则从非常简单的操作开始(即使需要更多请求,客户端连接也很容易),然后根据瓶颈所在来改进模型。

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

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