17

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

3 Answers 3

29

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

class MyModel
  include Mongoid::Document
  field :some_data, :type => Hash
end
Sign up to request clarification or add additional context in comments.

1 Comment

For latest releases: field :some_data, type: Hash
12

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

Comments

1

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

SomeMongoidObject['dynamic_attribute'] = json_data

3 Comments

Nice workaround, but I would still like to see mongoid supporting JSON fields as an option.
I have logged this as an issue on GitHub: github.com/mongoid/mongoid/issues/1603
Got feedback on the issue. Apparently you can just use field type Hash or embedded document. Have posted an answer as such here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.