Reputation: 526
I try to use datamapper and mongoid on my project. I followed the link https://github.com/solnic/dm-mongo-adapter. But there is no so much information. I assimilate to datamapper and sqlite3 adapter in this post: http://net.tutsplus.com/tutorials/ruby/ruby-for-newbies-working-with-datamapper/ Everything is ok with sqlite3, but i bogged down with mongodb.
When I run "ruby rm.db" in console i take "dm.rb:1:in `': uninitialized constant DataMapper (NameError)" error.
How can i resolve this problem? I added these gems in my gemfile below:
dm-core
dm-aggregates
dm-migrations
mongo
mongodb
mongo_ext
Then I added below code in a file named dm.rb in the root of project.
DataMapper.setup(:default,
:adapter => 'mongo',
:database => 'my_mongo_db',
)
# Define resources
class Student
include DataMapper::Mongo::Resource
property :id, ObjectId
property :name, String
property :age, Integer
end
class Course
include DataMapper::Mongo::Resource
property :id, ObjectId
property :name, String
end
# No need to (auto_)migrate!
biology = Course.create(:name => "Biology")
english = Course.create(:name => "English")
# Queries
Student.all(:age.gte => 20, :name => /oh/, :limit => 20, :order => [:age.asc])
# Array and Hash as a property
class Zoo
include DataMapper::Mongo::Resource
property :id, ObjectId
property :opening_hours, Hash
property :animals, Array
end
Zoo.create(
:opening_hours => { :weekend => '9am-8pm', :weekdays => '11am-8pm' },
:animals => [ "Marty", "Alex", "Gloria" ])
Zoo.all(:animals => 'Alex')
Upvotes: 1
Views: 1079
Reputation: 887
I'll answer this in two parts for you.
First, to address your current issue, the problem is that it doesn't look like you're requiring DataMapper before you try to use it. You can either require dm-mongo-adapter at the top of your rb file or since you're using bundler you could actually do this in your Gemfile
directly.
# add this to the beginning of your dm.rb file
require 'dm-mongo-adapter'
# or put this in your Gemfile, run with `bundle exec dm.rb`
gem 'dm-mongo-adapter', :require => true
Second, regarding the use of dm-mongo-adapter. There are a couple of problems with this approach that will probably be headaches for you both now and later on down the road.
MongoDB doesn't use SQL syntax for queries and it is a non-relational database. DataMapper as awesome as it is, is based entirely on SQL as a query language and all of its APIs and document modeling helpers were designed with relation data modeling in mind.
The mongo adapter you're using was designed to try and bridge that gap for developers who were used to SQL syntax but the two approaches are so dramatically different that you'll likely end up with sub-optimal performance due to poor queries, poor indexes and poor data models that weren't ever really designed to be used in a database like MongoDB.
I highly recommend checking out Mongoid or Mongo Mapper (or just using the mongo gem itself) rather than taking this approach.
Also, you should check out 10gen's website where there are a number of good talks and presentations about how MongoDB differs from traditional relational dbs and why understanding the differences before you build your application matters so much.
http://www.10gen.com/presentations/building-your-first-app-introduction-mongodb-0 http://www.10gen.com/presentations/schema-design-4
If you look at github for dm-mongo-adapter it doesn't appear to have been updated in over a year. This likely has a lot to do with what I just wrote above but will cause its own troubles as well. It's unlikely that you'll even be able to successfully use a version that old with a newer version of MongoDB and you definitely won't be able to take advantage of newer MongoDB features.
Upvotes: 1