0

我已经使用 MongoDB 创建了一个示例数据库,其中包含来自 mongodbs 网站上的 JSON 文件的数据,我已使用以下命令将其导入。mongoimport --db test --collection zips --file zips.json 数据正在处理中,当我尝试在控制台中搜索数据时,它会显示出来,到目前为止一切都很好。

我的问题是当我尝试将数据库与我的 Rails 应用程序一起使用时。我创建了一个类,城市,代码如下所示。

    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

我的 mongoid.yml 文件看起来像这样

    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

活动记录被禁用。当我启动 Rails 控制台并尝试 City.where... 时,我得到以下输出。

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

如果我尝试 City.first 则会引发错误

    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'

我已经在这里上传了我的代码,请克隆并尝试帮助我,我卡住了。如您所见,我的数据库不是空的。http://i.stack.imgur.com/Bho2H.png

4

1 回答 1

0

Mongoid(使用与 ActiveRecord 类似的映射约定)将类名复数以获取集合名称。看起来您有一个名为的集合,city但 Mongoid 会将您的类映射到一个名为cities.

您可以覆盖集合名称:

class City
  store_in collection: "city"
end

或者更好地将您的数据导入到城市集合中。

于 2013-04-17T13:06:07.567 回答