我在heroku实例上使用了mongolab和monogid。
我可以在rails控制台中访问我的模型并创建它们。
然而 - 调用Model.first
/ Model.find_or_initialize_by
会导致无限循环
MOPED: xx:29454 COMMAND database=admin command={:ismaster=>1} runtime: 7.8211ms
MOPED: xx:29454 QUERY database=heroku_zl6cgccm
collection=calculations selector={"$query"=>{}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 1.6873ms
MOPED: xx:29454 QUERY database=heroku_xx collection=calculations selector={"$query"=>{}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 5.3479ms
MOPED: xx:29454 QUERY database=heroku_xx collection=calculations selector={"$query"=>{}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 8.8738ms
一直到下来:
collection=calculations selector={"$query"=>{}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 1544.7719ms
SystemStackError: stack level too deep
from /app/vendor/bundle/ruby/2.2.0/gems/bson-3.2.4/lib/bson/string.rb:79:in `to_bson_cstring'
from /app/vendor/bundle/ruby/2.2.0/gems/bson-3.2.4/lib/bson/string.rb:63:in `to_bson_key'
from /app/vendor/bundle/ruby/2.2.0/gems/bson-3.2.4/lib/bson/symbol.rb:58:in `to_bson_key'
from /app/vendor/bundle/ruby/2.2.0/gems/bson-3.2.4/lib/bson/hash.rb:45:in `block (2 levels) in to_bson'
from /app/vendor/bundle/ruby/2.2.0/gems/bson-3.2.4/lib/bson/hash.rb:43:in `each'
from /app/vendor/bundle/ruby/2.2.0/gems/bson-3.2.4/lib/bson/hash.rb:43:in `block in to_bson'
from /app/vendor/bundle/ruby/2.2.0/gems/bson-3.2.4/lib/bson/encodable.rb:57:in `encode_with_placeholder_and_null'
from /app/vendor/bundle/ruby/2.2.0/gems/bson-3.2.4/lib/bson/hash.rb:42:in `to_bson'
from /app/vendor/bundle/ruby/2.2.0/gems/moped-2.0.7/lib/moped/protocol/message.rb:173:in `serialize_selector'
from /app/vendor/bundle/ruby/2.2.0/gems/moped-2.0.7/lib/moped/protocol/message.rb:325:in `serialize'
from /app/vendor/bundle/ruby/2.2.0/gems/moped-2.0.7/lib/moped/connection.rb:171:in `block in write'
from /app/vendor/bundle/ruby/2.2.0/gems/moped-2.0.7/lib/moped/connection.rb:169:in `each'
from /app/vendor/bundle/ruby/2.2.0/gems/moped-2.0.7/lib/moped/connection.rb:169:in `write'
from /app/vendor/bundle/ruby/2.2.0/gems/moped-2.0.7/lib/moped/authenticatable.rb:64:in `login'
from /app/vendor/bundle/ruby/2.2.0/gems/moped-2.0.7/lib/moped/operation/read.rb:55:in `block (2 levels) in execute'
from /app/vendor/bundle/ruby/2.2.0/gems/moped-2.0.7/lib/moped/node.rb:118:in `block in connection'
... 9285 levels...
from /app/vendor/bundle/ruby/2.2.0/gems/mongoid-4.0.2/lib/mongoid/query_cache.rb:190:in `first_with_cache'
from /app/vendor/bundle/ruby/2.2.0/gems/mongoid-4.0.2/lib/mongoid/contextual/mongo.rb:199:in `block (2 levels) in first'
from /app/vendor/bundle/ruby/2.2.0/gems/mongoid-4.0.2/lib/mongoid/contextual/mongo.rb:545:in `with_sorting'
from /app/vendor/bundle/ruby/2.2.0/gems/mongoid-4.0.2/lib/mongoid/contextual/mongo.rb:198:in `block in first'
from /app/vendor/bundle/ruby/2.2.0/gems/mongoid-4.0.2/lib/mongoid/contextual/mongo.rb:457:in `try_cache'
from /app/vendor/bundle/ruby/2.2.0/gems/mongoid-4.0.2/lib/mongoid/contextual/mongo.rb:197:in `first'
from /app/vendor/bundle/ruby/2.2.0/gems/mongoid-4.0.2/lib/mongoid/contextual.rb:20:in `first'
from /app/vendor/bundle/ruby/2.2.0/gems/mongoid-4.0.2/lib/mongoid/findable.rb:144:in `first'
from (irb):10
from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/commands/console.rb:110:in `start'
from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/commands/console.rb:9:in `start'
from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:68:in `console'
from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.4/lib/rails/commands.rb:17:in `<top (required)>'
from /app/bin/rails:8:in `require'
这是我的mongoid.yml
文件:
production:
sessions:
default:
uri: <%= ENV['MONGOLAB_URI'] %>
options:
max_retries: 30
retry_interval: 1
timeout: 15
refresh_interval: 10
答案 0 :(得分:3)
我遇到了与MongoLab类似的问题,无法进行数据库查询,直到我的堆栈级别太深。我的客户想要一个免费的数据库,所以我坚持了12小时的故障排除。
我尝试了上面的所有内容,以及其他一些配置选项,然后偶然发现: https://devcenter.heroku.com/articles/mongolab
我忽略了一条线:“如果您使用的是MongoDB 3.0版(所有MongoLab Sandbox都是3.0版),您必须使用Mongoid 5.x.”。我已经运行了一个捆绑更新,所以我没想太多。
你应该进入你的gem文件并指定:
gem'mongoid','〜&gt; 5.0'
我还必须删除我的mongoid.yml并重新开始
rails g mongoid:config
mongoid.yml将其嵌套更改为列出“客户端”而不是“会话”,这实际上会混淆Rails。
这就是为我解决的问题。
答案 1 :(得分:2)
它看起来(从您的数据库用户名)我们都使用Hero的MongoHQ插件,他们的最低层数据库是分片的,我相信一旦它到达就需要一个简单的配置更改来帮助查询路由正确。
#强制驱动程序集群以某种方式运行而不是自动运行 #discovering。可以是以下之一:: direct,:replica_set,:sharded。设置为:直接 #连接到副本集的隐藏成员时。 connect :: direct
http://docs.mongodb.org/ecosystem/tutorial/ruby-mongoid-tutorial/#anatomy-of-a-mongoid-config
我改为Mongo Compose插件没有分片,问题立即消失。
如果您想继续使用具有免费版本的MongoLab,请尝试修改配置,如:
options:
connect: :sharded
进一步阅读:http://docs.mongodb.org/manual/core/sharded-cluster-query-router/#broadcast-operations