I am currently trying to learn Ruby On Rails as I am a long-time PHP developer so I am building my own community like page.
I have came pretty far and have made the user models and such using MySQL.
But then I heard of MongoDB and looked in to it a little bit more and I find it kind of nice.
So I have set it up and I am using mongomapper for the connection between rails and MongoDB.
I'm now using it for the News page on the site.
I also have a profile page for every User which includes their own guestbook so other users can come to their profile and write a little message to them.
My thought now is to change the User models from using MySQL to start using MongoDB.
I can start by showing how the models for each User is set up.
The user model:
class User < ActiveRecord::Base
has_one :guestbook, :class_name => "User::Guestbook"
The Guestbook model model:
class User::Guestbook < ActiveRecord::Base
belongs_to :user
has_many :posts, :class_name => "User::Guestbook::Posts", :foreign_key => "user_id"
And then the Guestbook posts model:
class User::Guestbook::Posts < ActiveRecord::Base
belongs_to :guestbook, :class_name => "User::Guestbook"
I have divided it like this for my own convenience but now when i am going to try to migrate to MongoDB i dont know how to make the tables.
I would like to have one table for each user and in that table a "column" for all the guestbook entries since MongoDB can have a EmbeddedDocument. I would like to do this so i just have one Table for each user and not like now when i have three tables just to be able to have a guestbook.
So my thought is to have it like this:
The user model:
class User
include MongoMapper::Document
one :guestbook, :class_name => "User::Guestbook"
The Guestbook model model:
clas开发者_开发技巧s User::Guestbook
include MongoMapper::EmbeddedDocument
belongs_to :user
many :posts, :class_name => "User::Guestbook::Posts", :foreign_key => "user_id"
And then the Guestbook posts model:
class User::Guestbook::Posts
include MongoMapper::EmbeddedDocument
belongs_to :guestbook, :class_name => "User::Guestbook"
But then i can think of one problem.. That when i just want to fetch the user information like a nickname and a birthdate then it will have to fetch all the users guestbook posts. And if each user has like a thousand posts in the guestbook it will get really much to fetch for the system. Or am I wrong?
Do you think I should do it any other way?
No you don't have to fetch all the guestbook entries if you fetch a user, this is the mongo query for and MongoMapper shouldn't work so much differently (For myself, I'm using Mongoid):
db.users.find({_id: '21314'}, {guestbook: 0})
// instead of {guestbook: 1} which would return only the guestbook
Something to be aware of, MongoDB still has a limit of 4mb for a single document but that should be tens of thousands of guestbook entries. You might as well push older ones into a sort of archive.
If you are going to have thousands of guestbook entries then it would probably be a good idea to make that a separate collection (for exactly the reason that you've stated).
精彩评论