How to programmatically get the current database Mongoid is writing to?

只谈情不闲聊 提交于 2019-12-03 11:52:15
Vamshidhar Behara

Got it!

Mongoid.default_session.options[:database]

The new way to get this is

Mongoid::Config.clients["default"]["database"]

You can also just have a look at

Mongoid::Config.clients

to see what else is available.

If you want the overrided database you actually need to use

Mongoid::Threaded.database_override

Running Mongoid.default_session.options[:database] gives an error that default_session is invalid. It is now default_client. That works wonderfully.

Run it without the [:database] to get all the option and see the full configuration of your database.

Mongoid::Config.clients["default"]["database"] assumes you are NOT using the uri version of mongoid.yml. You will get nil in all cases. Use Mongoid::Config.clients["default"] to get the full configuration of the current database, or Mongoid::Config.clients["default"]['uri'] to give you just the full uri.

Pick the database name from the URI after the last port number/. There could be severalif using a sharded configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!