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.
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.
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)
精彩评论