我对Rails和mongodb有一个小问题。我有一个小应用程序,在开发模式下工作得很好。它也可以在生产模式下工作,但是当我进入rails控制台时,我看不到任何内容。
user-001@marcus:/var/www/webapp% rails c RAILS_ENV="production"
You did not specify how you would like Rails to report deprecation notices for your RAILS_ENV=production environment, please set config.active_support.deprecation to :log, :notify or :stderr at config/environments/RAILS_ENV=production.rb
Loading RAILS_ENV=production environment (Rails 3.2.1)
1.9.3p0 :001 > User.all
=> []
我的config / initialize / mongo.rb看起来像这样
MongoMapper.connection = Mongo::Connection.new('localhost', 27017)
MongoMapper.database = "webapp-#{Rails.env}"
if defined?(PhusionPassenger)
PhusionPassenger.on_event(:starting_worker_process) do |forked|
MongoMapper.connection.connect if forked
end
end
但它看起来也是空的
$ mongo localhost:27017/mail1up-production
MongoDB shell version: 1.8.2
Fri Feb 17 18:26:00 *** warning: spider monkey build without utf8 support. consider rebuilding with utf8 support
connecting to: localhost:27017/webapp-production
> db.mycollection.stats()
{ "errmsg" : "ns not found", "ok" : 0 }
>
我做错了吗?为什么我看不到数据库中的数据?我该怎么测试呢?
答案 0 :(得分:7)
我认为它可能与您的查询语法有关。我刚刚尝试使用正常的ActiveRecord查询。我正在使用MongoDB和Mongoid作为我的ORM。
这个问题与你的有关,虽然它也使用了Mongoid: mongoid-finders-not-working
我发现成功使用以下内容进行“全部”查询:
User.all.to_a
答案 1 :(得分:1)
您似乎需要运行mmconsole
。
然后将包含MongoMapper并将您的数据库设置为'mm-test',将其放入irb。
在您的情况下,您的mongo.rb初始化程序可能不受尊重,并且您已连接到mm-test数据库而不是“webapp - #{Rails.env}”。
我收到了一些这方面的信息here