【问题标题】:How can you map this to Mongoid or MongoMapper你怎么能把它映射到 Mongoid 或 MongoMapper
【发布时间】:2012-05-18 00:21:09
【问题描述】:

您好,谁能帮我把它映射到 Rails 和 Mongo,我有两天时间研究这个主题,但我似乎无法弄清楚,我的意思是在 Mongoid 或 MongoMapper 中为这两个集合制作模型。

Users collection

{ "_id" : ObjectId( "4fa883e45ddddbb60c4a6970" ),
   "username" : "joedoe",
   "name" : "joe",
   "created" : "2010-01-15 00:00:00",
   "suscribers" : [ 
      { "suscribe_id" : ObjectId( "4fa884a35ddddbb60c4a6971" ),
        "name" : "Mashable", 
        "tags" : ['Tech', 'News'], 
        "sort" : 0 
      }, 
      { "suscribe_id" : ObjectId( "4fa884a35ddddbb60c4a6972" ),
        "name" : "Joe", 
        "tags" : ['Tech'], 
        "sort" : 1 
      } 
   ] }

Suscribers collection
 { "_id" : ObjectId( "4fa884a35ddddbb60c4a6971" ),
   "name" : "Dan",
   "suscribers_count" : 0,
   "latest" : "2010-01-15 00:00:00" },
 { "_id" : ObjectId( "4fa884a35ddddbb60c4a6972" ),
   "name" : "Fer",
   "suscribers_count" : 0,
   "latest" : "2010-01-15 00:00:00" }

谢谢!

【问题讨论】:

  • 这是您需要在 Ruby 中访问的现有数据吗?
  • 是的,我在 mongodb 上有这些数据,但我不知道如何映射它来制作模型

标签: ruby-on-rails mongoid mongomapper


【解决方案1】:

我在想这样的事情:

class User
  include Mongoid::Document

  field :username
  field :name
  embeds_many :user_subscribers
end

class UserSubscriber
  include Mongoid::Document

  embedded_in :user
  belongs_to :subscriber
  field :tags, type: Array
  field :sort, type: Integer
end

class Subscriber
  include Mongoid::Document

  field :name
  has_many :user_subscribers
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多