将 MongoDB 与 Ruby On Rails 和 Mongomapper 插件结合使用

发布于 2024-08-31 13:24:47 字数 1726 浏览 6 评论 0原文

我目前正在尝试学习 Ruby On Rails,因为我是一名长期的 PHP 开发人员,所以我正在构建自己的社区页面。

我已经取得了很大的进展,并使用 MySQL 制作了用户模型等。

但后来我听说了 MongoDB,并进一步研究了它,我发现它很好。

所以我已经设置好了,并且使用 mongomapper 来连接 Rails 和 MongoDB。

我现在将其用于网站上的新闻页面。

我还为每个用户提供了一个个人资料页面,其中包括他们自己的留言簿,以便其他用户可以访问他们的个人资料并向他们写一条小消息。

我现在的想法是改变用户模型,从使用 MySQL 开始使用 MongoDB。

我可以首先展示如何设置每个用户的模型。

用户模型:

class User < ActiveRecord::Base
            has_one :guestbook, :class_name => "User::Guestbook"

留言簿模型 模型:

class User::Guestbook < ActiveRecord::Base
  belongs_to :user
  has_many :posts, :class_name => "User::Guestbook::Posts", :foreign_key => "user_id"

然后留言簿帖子模型:

class User::Guestbook::Posts < ActiveRecord::Base
  belongs_to :guestbook, :class_name => "User::Guestbook"

为了方便起见,我将其划分为这样,但现在当我要尝试迁移到 MongoDB 时,我不知道如何制作表。

我想为每个用户创建一个表,并在该表中为所有留言簿条目创建一个“列”,因为 MongoDB 可以有一个 EmbeddedDocument。我想这样做,所以我只为每个用户准备一张桌子,而不是像现在我有三张桌子只是为了能够拥有一本留言簿。

所以我的想法是这样的:

用户模型:

class User
  include MongoMapper::Document
  one :guestbook, :class_name => "User::Guestbook"

留言簿模型 模型:

class User::Guestbook
  include MongoMapper::EmbeddedDocument
  belongs_to :user
  many :posts, :class_name => "User::Guestbook::Posts", :foreign_key => "user_id"

然后留言簿帖子模型:

class User::Guestbook::Posts
  include MongoMapper::EmbeddedDocument
  belongs_to :guestbook, :class_name => "User::Guestbook"

但是我可以想到一个问题.. 当我只想获取用户信息(例如昵称)和生日,那么它将必须获取所有用户的留言簿帖子。如果每个用户在留言簿中都有大约一千个帖子,那么系统将获得非常多的信息。还是我错了?

你认为我应该采取其他方式吗?

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:

class 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一刻暧昧 2024-09-07 13:24:48

如果您将拥有数千条留言簿条目,那么将其作为一个单独的集合可能是一个好主意(正是出于您所说的原因)。

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).

金兰素衣 2024-09-07 13:24:47

不,如果您获取用户,则不必获取所有留言簿条目,这是 mongo 查询,并且 MongoMapper 的工作方式不应有太大不同(对于我自己,我正在使用 Mongoid):

db.users.find({_id: '21314'}, {guestbook: 0})
                // instead of {guestbook: 1} which would return only the guestbook

需要注意的事情, MongoDB 对单个文档的大小仍然有 4mb 的限制,但这应该是数以万计的留言簿条目。您不妨将较旧的内容放入某种存档中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文