文档菜单
文档首页
/ / /
Ruby MongoDB 驱动
/

数据库

本页内容

  • 列出集合
  • 任意命令
  • 删除数据库

该驱动程序为数据库对象提供各种辅助功能,用于执行命令、获取集合列表和执行管理任务。

要获取数据库中集合的列表或集合名称,请使用collectionscollection_names

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.

要在数据库上执行任何命令,请使用 command 方法。

client = Mongo::Client.new([ '127.0.0.1:27017' ], database: 'music')
database = client.database
result = database.command(:ping => 1)
result.first # Returns the BSON::Document returned from the server.

注意

同时指定服务器API版本作为客户端选项,以及指定 command 方法中的任何相应命令参数(即 apiVersionapiStrictapiDeprecationErrors 命令参数)是不允许的,将会产生错误。

要删除数据库,请使用 drop 方法。

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

返回

模式操作