为什么我收到错误“db_name 必须是字符串或符号”?

发布于 2024-11-29 20:19:40 字数 1439 浏览 0 评论 0原文

快速版本(适用于熟悉 MongoidSinatra):如果不是Psyche/Syck YAML 解析问题,为什么否则,当我尝试使用 Mongoid 连接到 MongoDB 数据库时,我可能会遇到此错误吗? (或者也许就是这个问题,在这种情况下,我该如何修复下面发布的 mongoid.yml 文件?)


更详细的(原始)版本

我有一个Sinatra 应用程序通过 Mongoid 与 MongoDB 数据库交互:

configure do
    Mongoid.load!('config/mongoid.yml')
end

我的 mongoid.yml 文件如下所示:

development:
    host: localhost
    database: project_development

test:
    host: localhost
    database: project_test

production:
    uri: <%= ENV['MONGOLAB_URI'] %>

每当我尝试以某种方式与数据库交互时,我都会收到错误 db_name 必须是字符串或符号

现在,我在 Google 上找到了大量关于此的信息;但我能找到的所有内容似乎都表明问题与 Ruby 现在使用 Psyche YAML 解析器而不是旧的 Syck 解析器有关。我认为这实际上与我的情况无关,因为据我所知,上面的 YAML 应该可以被任何一个完美解析。

(尽管如此,我已经尝试使用 YAML::ENGINE.yamler= 'syck' 技巧,但无济于事。我得到了完全相同的错误消息.)

当我将配置更改为:

Mongoid.configure do |config|
    name = "project_development"
    host = "localhost"
    config.master = Mongo::Connection.new.db(name)
end

...然后一切正常。所以我知道 MongoDB 正在我的机器上运行。特别是当我使用 YAML 文件时,事情就会出错。

那么什么给出呢?

Quick version (for those familiar with Mongoid & Sinatra): If it's not the Psyche/Syck YAML-parsing issue, why else might I get this error when trying to connect to a MongoDB database using Mongoid? (Or maybe it is that issue, in which case, how do I fix my mongoid.yml file, posted below?)


More detailed (original) version:

I have a Sinatra app interacting with a MongoDB database via Mongoid:

configure do
    Mongoid.load!('config/mongoid.yml')
end

And my mongoid.yml file looks like this:

development:
    host: localhost
    database: project_development

test:
    host: localhost
    database: project_test

production:
    uri: <%= ENV['MONGOLAB_URI'] %>

Whenever I try to interact with the database in some way, I get the error db_name must be a string or symbol.

Now, I have found plenty of information on Google about this; but everything I can find seems to indicate that the problem has to do with Ruby now using the Psyche YAML parser instead of the old Syck parser. I don't think that's actually relevant in my case because, as far as I can tell, the above YAML should be perfectly parsable by either.

(For what it's worth, though, I have tried using the YAML::ENGINE.yamler= 'syck' trick, to no avail. I got the exact same error message.)

When I change the configuration to this:

Mongoid.configure do |config|
    name = "project_development"
    host = "localhost"
    config.master = Mongo::Connection.new.db(name)
end

...then everything works fine. So I know that MongoDB is working on my machine. It's specifically when I use a YAML file that things go awry.

So what gives?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一场春暖 2024-12-06 20:19:40

确保 ENV['RACK_ENV'] 设置正确,因为 Mongoid.load! 在找不到 Rails.env 时会使用它代码>.

Make sure that ENV['RACK_ENV'] is set properly, as that is what Mongoid.load! uses if it doesn't find Rails.env.

属性 2024-12-06 20:19:40

我在 Rails 上遇到了同样的问题,并以这种方式解决了它:

问题在于 Mongoid 和 MongodbLogger 对 mongoid.yml 结构的不同期望。
简而言之:只需在上层添加数据库即可:

development:
  sessions:
    default:
      database: apollo_development                       <<<< for the rest
      hosts:
        - localhost:27017
      options:
        consistency: :strong
  database: apollo_development                           <<<<main level for logger
    .... more stuff for the logger

优点是您可以使用一个数据库来存储数据,另一个数据库用于记录器。
从技术上讲,有两个使用 yml 的独立系统:记录器和 MongoId。

I had the same problem with rails and solved it in this way :

The Problem lies in the different expectations of Mongoid and MongodbLogger concerning the structure of the mongoid.yml.
Short : just add the database in addition on the upper level :

development:
  sessions:
    default:
      database: apollo_development                       <<<< for the rest
      hosts:
        - localhost:27017
      options:
        consistency: :strong
  database: apollo_development                           <<<<main level for logger
    .... more stuff for the logger

The advantage is that you can use a database for your data and another for the logger.
Technical there are two independant systems using the yml : the logger and MongoId.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文