MongoDB 我应该制作另一个集合还是应该嵌入它
我正在制作一个基于 node js 的音乐应用程序,我选择了 mongodb 作为它的数据库.我知道 mongodb 操作的基本语法,但我是架构设计的新手.mongoDB之前没学过任何数据库软件.
I am making a music app fron node js and i choosed mongodb as database for it. I know basic syntax for mongodb operations but i am new to schema designs. I havent learnt any database software before mongoDB.
所以在这个应用程序中,每次用户喜欢一首歌时,我都会将这首歌的所有重要数据存储在名为 tracker
和 liked
的单独集合中,因此它将是更容易制作推荐算法.users
集合架构是这样的:
So in this app, every time the user likes a song, i store all the important data of that song in seperate collection called tracker
and liked
so it will be easier to make recommendation algorithm. The users
collection schema is like this:
{
uid: Unique Custom Ids,
private: {
email: email,
phone: number,
},
public: {
profile: {
info: {
bio: String,
name: String,
username: String
},
src: {
pfpSrc: Url
}
}
}
}
我的跟踪器集合架构如下所示:
And my tracker collections schema looks like:
{
uid: custom uid,
byLiked: {
genre: {
genreName: "No of time user liked this genre"
},
tags: {
tag: "No of time user liked this tag"
}
},
byPlays: {
genre: {
genreName: "No of time user liked this genre"
},
tags: {
tag: "No of time user liked this tag"
}
}
}
liked
集合架构如下所示:
{
uid: "custom uid",
tracks: [
{song_id: "custom song id"},
...
]
}
我应该这样做还是将所有这些集合嵌入users
集合的文档中?
Should i do in this way or embed all these collection in documents of users
collection?
考虑以下内容.
基本上有 3 个标准,您可以根据这些标准来决定是否应该嵌入或引用相关实体.
There is basically 3 criterias, on which you can decide, whether you should embed or reference the related entity.
这个标准是:
- 关系类型,
- 数据访问模式(读取和写入数据的频率),
- 数据接近度(数据相关多少",我们希望如何查询)
评估时,您应该结合所有这三个标准.
When evaluate, you should combine all this three criterias.
在以下情况下选择嵌入:
Choose embedding if:
- 关系是一对多或一对多(不是太多)
- 数据主要是读取,不会快速变化
- 数据集真正属于一起
在以下情况下选择引用:
Choose referencing if:
- 关系是一对多,一对多(基于文档的数据库中的新术语),多对多
- 数据更新很多
- 两个数据集都需要自己经常查询
查看您的场景,我强烈建议您选择引用.
Looking over your scenario, I highly recommend to choose referencing.