【问题标题】:MongoDB Making my schema structure less relationalMongoDB 使我的模式结构不那么相关
【发布时间】:2019-05-01 15:00:45
【问题描述】:

我一直在努力构建我的 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 之类的东西。

【问题讨论】:

  • 我认为除了UserProfilePosts 集合之外,您不需要任何其他东西。我没有遵循Uploads 架构的想法,但如果这些是上传到帖子的文件,那么它们应该是Posts 中的一个数组。 User & Profile 应该是一个模型,因为我认为没有理由将它们分开。

标签: mongodb mongoose nosql schema


【解决方案1】:

正如 Akrion 在上面的评论中也没有提到,第一步是结合用户和配置文件模式。我之前没有考虑过的另一件事是什么信息被汇集在一起​​。新的 UserProfile 架构不应包含诸如 cmets/posts/uploads/likes 之类的内容,因为这些内容最好作为 Post 文档的一部分存在。我的问题是认为在我的 UserProfile 中记录这些内容会在我需要它们时优化这些项目的检索,但我最终使我的 UserProfile 架构膨胀。通过创建者而不是通过从 UserProfile 文档中获取的 ID 搜索帖子集合并没有什么不同。上传文档现在不再是 GridFS 模式,而是文件系统中的文件记录,但这与我原来的问题不太相关。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-20
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 2015-07-29
    • 1970-01-01
    • 1970-01-01
    • 2010-12-21
    相关资源
    最近更新 更多