Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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. http://i.stack.imgur.com/Bho2H.png

share|improve this question
Why do you use the store_in ? – Guy Apr 17 at 11:29
Oh, I've removed that, it was from a tutorial. Now I Get "nil" as an answer. But I KNOW that there's data in the db, it works when I use the mongo console.. – Josefffs Apr 17 at 11:39
Do City.count and City.all.to_a and paste back the result :-) – Guy Apr 17 at 11:42
City.count = 0, City.all.to_a = [] I dont understand, where do I connect to the database? I thought MongoID did that for me? – Josefffs Apr 17 at 12:04
Then the city collection is empty mate – Guy Apr 17 at 12:16
show 3 more comments

1 Answer

up vote 0 down vote accepted

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.

share|improve this answer
My collection is named "city". When using Mongodb console, I can write db.city.findOne, I cant write db.cities.findOne. Are u sure this is the problem? – Josefffs Apr 17 at 13:19
Yes I think so. Mongoid is looking for a collection called cities. If you don't want to change the database then it should be easy to try to add the store_in declaration to your model. – Steve Apr 17 at 13:32
It solved it, thank you very much :) I used the store_in in my code before, but it was named "City", not "city" ;) Cheers! – Josefffs Apr 17 at 13:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.