【问题标题】:MongoDB: Efficient schema design with embedded documentsMongoDB:嵌入文档的高效模式设计
【发布时间】:2011-08-25 18:49:33
【问题描述】:

我对 NoSQL 很陌生,我正在努力解决它。作为一个例子,我试图为一个简单的博客设计一个模式,该博客的作者有帖子,有 cmets。像这样:

Author
name : String,
email : String,
posts : [Post]

Post
title : String,
body : String,
comments : [Comment]

Comment 
commenter : String,
comment : String

所以这似乎是设计模式的最不规范的方式。当我想获取作者帖子的列表时,它非常有用,但是当我尝试按标题查询帖子时遇到问题。这将返回作者对象和该作者的所有帖子。然后我可以在帖子中搜索我想要的帖子,但这似乎效率低下。

处理这种模式的最有效方法是什么?我应该只有一个 Posts 对象并使作者成为 Post 对象中的一个字段(或嵌入式文档)吗?或者最好将数据存储在多个位置?

我花了这么多年时间试图规范化关系数据库,以至于我似乎无法以 NoSQL 方式思考。任何建议将不胜感激。

【问题讨论】:

    标签: mongodb nosql


    【解决方案1】:
    Post
     title: String
     author: String
     comment: String
     posted: Date
    
    Author
     name: String
     email: String
    

    如果您的模型的“核心”是帖子,那么为什么不做到这一点,第一。您可以按标题、作者和日期搜索帖子。

    【讨论】:

      【解决方案2】:

      非规范化并不意味着禁止外键。

      我认为您绝对应该通过 ID 引用您的作者。但是,这就是非规范化的用武之地,您希望将作者姓名存储在Author 中的Post 对象中。这样,您就无需加入 AuthorPost 集合。

      Post
        title: string
        body: string
        authorName: string
        authorId: [id of author]
        comments: list of [Comment]
        created: date
        modified: date
      
      Author
        name: string
        email: string
      
      Comment
        subject: string
        body: string
        author: string (if you want anon comments)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-13
        • 1970-01-01
        • 1970-01-01
        • 2021-10-04
        • 1970-01-01
        • 2021-08-28
        相关资源
        最近更新 更多