问题描述
我正在使用Rails 3和Mongoid宝石.但是我需要用mongodb数据库列表填充组合框.在mongodb shell中,我们可以使用"show dbs"命令列出数据库.在mongodb驱动程序中,还有getDBNameList()和db.getCollectionNames()命令.但是我不知道如何从Rails应用程序的ruby中使用这些命令.
I am using Rails 3 and Mongoid gem. But I need to fill a combobox with the list of mongodb databases. In mongodb shell we can list databases with "show dbs" command. Also there is getDBNameList() and db.getCollectionNames() commands in mongodb drivers. But I could not figure out how to use these commands from a ruby on rails app.
我也想知道;如果我可以使用mongoid gem获得数据库和收藏夹列表.因为我确信我已经读过mongoid支持使用多个数据库,但是我认为它与模型有关.
Also I wonder; if I can get databases and collections list with using mongoid gem. Because I am sure that I had read that mongoid supports using more than one database, but I think it was model dependent.
那你怎么看?有什么解决办法吗,或者我必须使用mongo-ruby-driver gem,而不是mongoid.
So what do you think; is there any solution or I have to use mongo-ruby-driver gem, not mongoid.
推荐答案
在Mongoid 3中
In mongoid 3
Mongoid.default_session.collections # returns the collections
我通常按以下方式提取名称:
I usually extract the names as follows:
Mongoid.default_session.collections.map(&:name).sort
这篇关于如何从Rails应用程序上的ruby获取mongodb数据库列表和集合列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!