2011-08-25 69 views
1

我对NoSQL非常陌生,我试图围绕它进行思考。作为一个例子,我试图为一个简单的博客设计架构,该博客的作者有帖子,有评论。像这样:MongoDB:嵌入式文档的高效模式设计

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

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

Comment 
commenter : String, 
comment : String 

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

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

我花了这么多年来试图规范化关系数据库,我似乎无法用NoSQL的方式来思考。任何意见,将不胜感激。

回答

0
Post 
title: String 
author: String 
comment: String 
posted: Date 

Author 
name: String 
email: String 

如果你的模型的“核心”这里是交那么为什么不让它如此,一号。您可以按标题,按作者和按日期搜索帖子。

0

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

我想你应该肯定有一个Id的作者参考。但是,这是反规范化进来的地方,您希望将作者名称存储在Post对象中的Author中。这样,您不需要加入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)