MongoDB使我的架构结构的关系减少

时间:2018-11-29 14:08:36

标签: mongodb mongoose nosql schema

我一直在努力构建mongodb,但我只是意识到我实际上并没有花时间思考最佳策略,因此我可能会滥用nosql结构并构建更传统的关系结构。我正在建立一个社区论坛,到目前为止,我的收藏如下:

User - stores all user settings/data such as email, name, password, date joined, email/notification preferences, etc..

Profile - stores handle, gender, user location, forum rank, interests and then several arrays containing id's of things like an array of follower id's, array of post ids, array of upload id's, array of club id's, array of posts the user has liked, etc.

Posts - stores comment data, creator user id, category and then has an array of id's to uploaded files and an array of user id's for likes.

Uploads - GridFS schema to use when uploading files

我现在意识到,其他集合中所有这些id的事物数组的行为都更像是一个关系数据库,尤其是Profile模式,它基本上只是其他集合中id的集合。您能对我正在创建的数据库类型以及如何进行改进提供任何建议吗?例如,我是否应该有一个直接包含所有帖子和个人资料数据的用户模式,而不是将ID存储到不同集合中的单独模式中?当我使用这个项目进行学习时,我真的很想继续使用mongodb而不是转向MySQL之类的东西。

1 个答案:

答案 0 :(得分:0)

正如上面的评论中也没有提到Akrion一样,第一步是将User和Profile模式结合起来。我以前没有想过的另一件事是什么信息被召集在一起。新的UserProfile模式不应包含评论/帖子/上传/喜欢之类的内容,因为这些内容最好作为Post文档的一部分存在。我的问题是,在我的UserProfile中记录这些内容会在需要时优化对这些项目的检索,但最终导致UserProfile模式膨胀。按创建者搜索帖子集合,而不是按从UserProfile文档中获取的ID进行搜索,没有什么不同。现在,“上载”文档不再是GridFS架构,而是文件系统中文件的记录,但这与我的原始问题不太相关。