数据库
该驱动程序为数据库对象提供各种辅助功能,用于执行命令、获取集合列表和执行管理任务。
列出集合
要获取数据库中集合的列表或集合名称,请使用collections
和 collection_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
方法中的任何相应命令参数(即 apiVersion
、apiStrict
和 apiDeprecationErrors
命令参数)是不允许的,将会产生错误。
删除数据库
要删除数据库,请使用 drop
方法。
client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'music') client.database.drop