views:

29

answers:

0

I have a MongoDb database called Northwind. The database was created using C# driver. It contains 10-20 rows. Now, I want to get all those rows out from Ruby. I am using MongoMapper but how can I map a Ruby class "Category" to the "Name" field in the document.

require 'mongo_mapper'

class Category

include MongoMapper::Document

key :Name, String
key :NumberOfProducts, Integer   

end


require 'rubygems'
require 'mongo'
require 'mongo_mapper'
require 'category'

include Mongo

MongoMapper.database = 'Northwind'

db = Connection.new.db("Northwind")
coll = db.collection("categories")

category = Category.new()


category = coll.find_one()

puts category.Name  # This does not work

UPDATE:

Here is the solution:

require 'rubygems'
require 'mongo'
require 'mongo_mapper'
require 'category'

include Mongo

MongoMapper.database = 'Northwind'

category = Category.first()

puts category.Name