Rails:在MongoDB中存储JSON

Bor*_*ris 15 ruby-on-rails mongodb mongomapper mongoid

我从远程站点获取多个类似的JSON对象,并希望将它们存储在本地MongoDB中.

最好的方法是什么?(最好通过Mongoid或Mongo-mapper宝石)

谢谢

myd*_*rms 24

您可以使用Hash类型的mongoid字段或嵌入文档.

class MyModel
  include Mongoid::Document
  field :some_data, :type => Hash
end
Run Code Online (Sandbox Code Playgroud)


shi*_*ara 12

如果您只想在Mongo中存储JSON,则不需要Mongoid或MongoMapper.只需使用Mongo-ruby-driver

require 'mongo'

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

这样你就可以在收集测试中存储在数据库sample-db中