Search code examples
ruby-on-rails-3mongodbmongoidmongoid3

Ruby on Rails + MongoDB and MongoID


I have created a sample DB with MongoDB with data from a JSON file on mongodbs website I have imported it with the following command. mongoimport --db test --collection zips --file zips.json The data is being processed and when I try to search for data in the console it will show up, so far so good.

My problem is when I'm trying to use the DB with my Rails app. I have created a Class, City, the code looks like this.

    class City
       include Mongoid::Document
       field :c, as: :city, type: String
       field :l, as: :loc, type: Array
       field :p, as: :population, type: Integer
       field :s, as: :state, type: String
       field :_id, type: Integer
    end

And my mongoid.yml file looks like this

    development:
     # Configure available database sessions. (required)
    sessions:
     # Defines the default session. (required)
    default:
     # Defines the name of the default database that Mongoid can connect to.
     # (required).
    database: exjobb
     # Provides the hosts the default session can connect to. Must be an array
     # of host:port pairs. (required)
    hosts:
      - localhost:27017
    options:

    options:

    test:
     sessions:
      default:
       database: exjobb
       hosts:
        - localhost:27017
       options:
       consistency: :strong
        # In the test environment we lower the retries and retry interval to
        # low amounts for fast failures.
        max_retries: 1
        retry_interval: 0

Activerecord is disabled. When Im starting rails console and try City.where... I get the following output.

    City.where(city: "Acmar")
    => #<Mongoid::Criteria
    selector: {"c"=>"Acmar"}
    options:  {}
    class:    City
    embedded: false>

And if I try City.first an error is thrown

    NoMethodError: undefined method `to_sym' for nil:NilClass
from /Users/admin/.rvm/gems/ruby-1.9.3-p374/gems/mongoid-3.1.2/lib/mongoid/sessions.rb:409:in `__evaluate__'
from /Users/admin/.rvm/gems/ruby-1.9.3-p374/gems/mongoid-3.1.2/lib/mongoid/sessions.rb:357:in `__database_name__'
from /Users/admin/.rvm/gems/ruby-1.9.3-p374/gems/mongoid-3.1.2/lib/mongoid/sessions.rb:198:in `database_name'
from /Users/admin/.rvm/gems/ruby-1.9.3-p374/gems/mongoid-3.1.2/lib/mongoid/sessions.rb:429:in `current_database_name'
from /Users/admin/.rvm/gems/ruby-1.9.3-p374/gems/mongoid-3.1.2/lib/mongoid/sessions.rb:228:in `mongo_session'
from /Users/admin/.rvm/gems/ruby-1.9.3-p374/gems/mongoid-3.1.2/lib/mongoid/sessions.rb:171:in `collection'
from /Users/admin/.rvm/gems/ruby-1.9.3-p374/gems/mongoid-3.1.2/lib/mongoid/contextual/mongo.rb:256:in `initialize'
from /Users/admin/.rvm/gems/ruby-1.9.3-p374/gems/mongoid-3.1.2/lib/mongoid/contextual.rb:48:in `new'
from /Users/admin/.rvm/gems/ruby-1.9.3-p374/gems/mongoid-3.1.2/lib/mongoid/contextual.rb:48:in `create_context'
from /Users/admin/.rvm/gems/ruby-1.9.3-p374/gems/mongoid-3.1.2/lib/mongoid/contextual.rb:31:in `context'
from /Users/admin/.rvm/gems/ruby-1.9.3-p374/gems/mongoid-3.1.2/lib/mongoid/contextual.rb:21:in `rescue in first'
from /Users/admin/.rvm/gems/ruby-1.9.3-p374/gems/mongoid-3.1.2/lib/mongoid/contextual.rb:19:in `first'
from /Users/admin/.rvm/gems/ruby-1.9.3-p374/gems/mongoid-3.1.2/lib/mongoid/finders.rb:117:in `first'
from (irb):1
from /Users/admin/.rvm/gems/ruby-1.9.3-p374/gems/railties-3.2.11/lib/rails/commands/console.rb:47:in `start'
from /Users/admin/.rvm/gems/ruby-1.9.3-p374/gems/railties-3.2.11/lib/rails/commands/console.rb:8:in `start'
from /Users/admin/.rvm/gems/ruby-1.9.3-p374/gems/railties-    3.2.11/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'

I've uploaded my code here, please clone and try to help me, im stuck. As you can see, my db is not empty. https://i.sstatic.net/Bho2H.png


Solution

  • Mongoid (using similar mapping conventions to ActiveRecord) pluralizes the class name to get the collection name. It looks like you have a collection called city but Mongoid will have mapped your class to a collection called cities.

    You can override the collection name:

    class City
      store_in collection: "city"
    end
    

    or better still import your data into a cities collection.