I'm just getting started with MongoDB and Mongoid for Rails and need some advice on the proper design of a simple blog database.
I am currently using the structure below, but I need a way to request all comments written by this user (the equivalent of the relational db would be Comment.where('user_id = ?', user_id) ).
Is this set up correctly or do I need to move the comments into my own document and not insert them in the messages (as in the relational db schema)?
Appreciate any advice, thanks.
Database schema
post { _id: (object id) title: string body: string user_id: reference comments: [ { _id: (object id), body: string, user_id: reference }, { _id: (object id), body: string, user_id: reference }, ... ] } user { _id: (object id) name: string }
In MongoDB, my respective models are:
class Post include Mongoid::Document field :title field :body embeds_many :comments references_one :user end class Comment include Mongoid::Document field :body embedded_in :post references_one :user end class User include Mongoid::Document field :name references_many :posts end
source share