12

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.

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.

So what do you think; is there any solution or I have to use mongo-ruby-driver gem, not mongoid.

5 Answers 5

16

In mongoid 3

Mongoid.default_session.collections # returns the collections

I usually extract the names as follows:

Mongoid.default_session.collections.map(&:name).sort
Sign up to request clarification or add additional context in comments.

1 Comment

default_session is default_client in mongo 5
10

You can do the following using the mongo ruby driver:

require 'rubygems'
require 'mongo'

connection = Mongo::Connection.new("localhost")
connection.database_names.each do |name|
  db = connection.db(name)
  db.collections.each do |collection|
    puts "#{name} - #{collection.name}"
  end
end

Comments

3

It would be easier to get the Mongo::DB out of the Mongoid config:

db = Mongoid::Config.master
db.collection_names

5 Comments

we can configure all the databases in Mongoid (via mongoid.yml) dbs = Mongoid.databases dbs.each do |db| db.collections end
connection = Mongoid.master.connection connection.database_names #=> Get an array of names db = connection.database("name") #=> Get a specific db object db.collections #=> Get an array of collections ##### I think this is the best way; Thanks to durran (from github).
That doesn't seem to work for me (on mongoid 3.x): NoMethodError: undefined method `master' for Mongoid::Config:Module
I had the same problem than @turboladen, any ideas how to solve it?
@DavidSilveira: unfortunately I couldn't tell you if I did or not--it's been a long time since I've worked with that app. Good luck to you though!
2

A short version.

db = Mongoid.master
db.collection_names

1 Comment

How would you do this with Mongoid 3.0.0?
0

I used following code to list all collection and fields, provided the list of mongo db databases mentioned in mongoid.yml

def printDb(dbUri)
    puts "#{dbUri}"
    client = Mongo::Client.new(dbUri)
    # List all collections in the current database
    #collections = client.database.list_collections.map(&:name)
    collections = client.database.list_collections
    # Print the collection names
    collections.each do |collection|
            begin
                puts "  collection- #{collection[:name]}"
                colln=client[ collection[:name] ]
                #fields = collection.find_one.keys
                #fields = collection.find.limit(1).keys
                fields = colln.find.first.keys
            puts "      Fields: #{fields}"
            puts "" 
        rescue
            puts "      ** error in looking for a collection **"
            puts "" 
        end
    end
end

uriList=['mongodb://127.0.0.1:27017/my_app_db1',
    'mongodb://127.0.0.1:27017/my_app_db2', 
    'mongodb://127.0.0.1:27017/my_app_db3', 
    'mongodb://127.0.0.1:27017/my_app_db4'
     ]

#call function 
uriList.each do |myUri|
    printDb(myUri)
end

The parts of URI can be changed as per your yml file configurations. It may require mongo and mongoid gems

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.