开发者

Rails: Store JSON in MongoDB

I am getting multiple similar JSON object from a remote site and looking to store them in a local MongoDB.

What would be the best way to开发者_如何转开发 do this ? (Preferably via Mongoid or Mongo-mapper gems)

Thanks


You can use a mongoid field of type Hash or an embedded document.

class MyModel
  include Mongoid::Document
  field :some_data, :type => Hash
end


If you just want store your JSON in Mongo you don't need Mongoid or MongoMapper. Just use the Mongo-ruby-driver

require 'mongo'

db   = Mongo::Connection.new.db('sample-db')
coll = db.collection('test')
coll.insert(ActiveSupport::JSON.decode(you_json))

With that you store in database sample-db in collection test


Found out I can just put data directly into mongoid without defining the fields:

SomeMongoidObject['dynamic_attribute'] = json_data

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜