加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 关于简单MongoDB数据库结构的建议

发布时间:2020-12-17 02:28:24 所属栏目:百科 来源:网络整理
导读:我刚开始使用MongoDB和Mongoid for Rails,需要一些关于设计简单博客数据库的正确方法的建议. 我目前正在使用下面的结构,但我需要一种方法来查询给定用户编写的所有注释(关系数据库等效项将是Comment.where(‘user_id =?’,user_id)). 这是正确的设置,还是我
我刚开始使用MongoDB和Mongoid for Rails,需要一些关于设计简单博客数据库的正确方法的建议.

我目前正在使用下面的结构,但我需要一种方法来查询给定用户编写的所有注释(关系数据库等效项将是Comment.where(‘user_id =?’,user_id)).

这是正确的设置,还是我应该将注释移到他们自己的文档中,而不是将它们嵌入到帖子中(就像我在关系数据库模式中那样)?

感谢任何建议,谢谢.

数据库架构

post {
  _id: (object id)
  title: string
  body: string
  user_id: reference
  comments: [
    { _id: (object id),body: string,user_id: reference },{ _id: (object id),...
  ]
}

user {
  _id: (object id)
  name: string
}

在MongoDB中,我的相应模型是:

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

解决方法

Mongodb.org上有一篇很棒的文章,关于建模评论的各种选择.

退房:http://www.mongodb.org/display/DOCS/MongoDB+Data+Modeling+and+Rails#MongoDBDataModelingandRails-ModelingComments

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读