Navigation

Databases

On this page

Databases

The driver provides various helpers on database objects for executing commands, getting collection lists, and administrative tasks.

List Collections

To get a list of collections or collection names for a database, use collections and collection_names, respectively.

client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'music')
database = client.database

database.collections      # Returns an array of Collection objects.
database.collection_names # Returns an array of collection names as strings.

Arbitrary Comands

To execute any command on the database, use the command method.

client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'music')
database = client.database

result = database.command(:ismaster => 1)
result.first # Returns the BSON::Document returned from the server.

Drop Database

To drop a database, use the drop method.

client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'music')
client.database.drop
←   Change Streams Monitoring  →