MongoDB: Efficient schema design with embedded doc

2019-08-08 17:33发布

I'm very new to NoSQL and I'm trying to wrap my head around it. As an example I am trying to design a schema for a simple blog that has authors who have posts, which have comments. Like so:

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

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

Comment 
commenter : String,
comment : String

So this seems to be the most de-normalized way to design the schema. It works great when I want to get a list of an authors posts, but I run into problems when I try to query a post by it's title. This returns the author object and all that author's posts. I can then search the posts for the one I want, but that seems inefficient.

What is the most efficient way to handle this kind of schema? Should I only have a Posts object and make the author a field (or embedded doc) in the Post object? Or perhaps it's best to store the data in multiple locations?

I've spent so many years trying to normalize relational databases that I can't seem to think in the NoSQL way. Any advice would be appreciated.

标签: mongodb nosql
2条回答
叼着烟拽天下
2楼-- · 2019-08-08 18:14
Post
 title: String
 author: String
 comment: String
 posted: Date

Author
 name: String
 email: String

If the 'core' of your model here is the post then why not make it so, Number One. You can search posts by title, by author and by date.

查看更多
淡お忘
3楼-- · 2019-08-08 18:29

Denormalization does not mean foreign keys are forbidden.

I think you should definitely have a reference to your author by Id. However, and that is where denormalization comes in, you want to store the author name in the Author and in the Post objects. This way, you don't need to join the Author and Post collections.

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)
查看更多
登录 后发表回答