Overtone
Overtone

Reputation: 307

Getting an object from mongoDB with Mongoid

Simple enough situation. I've got a MongoDB database with a bunch of information from a previous developer. However I have limited information on the model that came before hand and I DONT have access to the original model class. I've been tinkering with the MongoDB driver to get some more information on it (MongoID will have to be used eventually to map the object back out) as follows.

#The flow is as follows
  #Connection
  #Databases
  #Database
  #Collection
  #Hash Info

  #Setup the connection. you can supply attributes in the form of ("db",portno) but most of the time it will pick up the defaults
  conn = Mongo::Connection.new

  #Database info
  mongodbinfo =conn.database_names
  conn.database_info.each { |info| puts info.inspect }


  db = conn.db("db_name_here")
  db.collection_names.each { |collection| puts collection.inspect  }


  collection = db.collection("model_name_here")
  puts collection.inspect

  collection.find.each { |row|
    puts row.inspect
    puts row.class
  }

Each row is a separate object and as MongoDB works, each object/document is a BSON object.

So the bottom line question is How do i de-serialize the BSON into a model using mongoID?

P.s Feel free to use the above code if your trying to figure out a new mongoDB, its been handy for debugging IMHO.

Upvotes: 0

Views: 269

Answers (1)

Overtone
Overtone

Reputation: 307

So this was a bust.

In the end I used the Mondb driver to manually pull the data out with queries. However creating the object was far more difficult.

Its better to have the actual model when using ORM.

Upvotes: 2

Related Questions