如何以编程方式访问 Mongoid 的配置?
How to programmatically access Mongoid's config?
具体来说,我想读取当前 Mongoid 配置的 hosts
值。
这可能吗?
我在 Rails 上使用 Mongoid 和 Ruby。
您可以使用以下方法实现此目的:
config = YAML::load(File.read(Rails.root.join('config/mongoid.yml')))
这会给你以下输出
{"development"=>
{"sessions"=>
{"default"=>
{"database"=>"analytics_new_development",
"hosts"=>["localhost:27017"],
"options"=>nil}},
"options"=>{"raise_not_found_error"=>false}},
"test"=>
{"sessions"=>
{"default"=>
{"database"=>"mongodemo_test",
"hosts"=>["localhost:27017"],
"options"=>
{"consistency"=>:strong, "max_retries"=>1, "retry_interval"=>0}}}},
"production"=>
{"sessions"=>
{"default"=>
{"database"=>"analytics_aws_production",
"hosts"=>["180.149.246.126"],
"options"=>nil}},
"options"=>{"raise_not_found_error"=>false}}}
使用上面的散列你可以取出你需要的期望值。
具体来说,我想读取当前 Mongoid 配置的 hosts
值。
这可能吗?
我在 Rails 上使用 Mongoid 和 Ruby。
您可以使用以下方法实现此目的:
config = YAML::load(File.read(Rails.root.join('config/mongoid.yml')))
这会给你以下输出
{"development"=>
{"sessions"=>
{"default"=>
{"database"=>"analytics_new_development",
"hosts"=>["localhost:27017"],
"options"=>nil}},
"options"=>{"raise_not_found_error"=>false}},
"test"=>
{"sessions"=>
{"default"=>
{"database"=>"mongodemo_test",
"hosts"=>["localhost:27017"],
"options"=>
{"consistency"=>:strong, "max_retries"=>1, "retry_interval"=>0}}}},
"production"=>
{"sessions"=>
{"default"=>
{"database"=>"analytics_aws_production",
"hosts"=>["180.149.246.126"],
"options"=>nil}},
"options"=>{"raise_not_found_error"=>false}}}
使用上面的散列你可以取出你需要的期望值。