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.
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 thePost
objects. This way, you don't need to join theAuthor
andPost
collections.