Class: Mongo::Database
- Inherits:
-
Object
- Object
- Mongo::Database
- Extended by:
- Forwardable
- Defined in:
- lib/mongo/database.rb,
lib/mongo/database/view.rb
Overview
Represents a database on the db server and operations that can execute on it at this level.
Defined Under Namespace
Classes: View
Constant Summary collapse
- ADMIN =
The admin database name.
'admin'.freeze
- COMMAND =
The “collection” that database commands operate against.
'$cmd'.freeze
- DEFAULT_OPTIONS =
The default database options.
Options::Redacted.new(:database => ADMIN).freeze
- NAME =
Database name field constant.
'name'.freeze
- DATABASES =
Databases constant.
'databases'.freeze
- NAMESPACES =
The name of the collection that holds all the collection names.
'system.namespaces'.freeze
Instance Attribute Summary collapse
-
#client ⇒ Client
readonly
Client The database client.
-
#name ⇒ String
readonly
Name The name of the database.
-
#options ⇒ Hash
readonly
Options The options.
Class Method Summary collapse
-
.create(client) ⇒ Database
private
Create a database for the provided client, for use when we don’t want the client’s original database instance to be the same.
Instance Method Summary collapse
-
#==(other) ⇒ true, false
Check equality of the database object against another.
-
#[](collection_name, options = {}) ⇒ Mongo::Collection
(also: #collection)
Get a collection in this database by the provided name.
-
#collection_names(options = {}) ⇒ Array<String>
Get all the names of the non system collections in the database.
-
#collections ⇒ Array<Mongo::Collection>
Get all the collections that belong to this database.
-
#command(operation, opts = {}) ⇒ Hash
Execute a command on the database.
-
#drop ⇒ Result
Drop the database and all its associated information.
-
#fs(options = {}) ⇒ Grid::FSBucket
Get the Grid “filesystem” for this database.
-
#initialize(client, name, options = {}) ⇒ Database
constructor
Instantiate a new database object.
-
#inspect ⇒ String
Get a pretty printed string inspection for the database.
-
#list_collections ⇒ Array<Hash>
Get info on all the collections in the database.
-
#users ⇒ View::User
Get the user view for this database.
Constructor Details
#initialize(client, name, options = {}) ⇒ Database
Instantiate a new database object.
185 186 187 188 189 190 |
# File 'lib/mongo/database.rb', line 185 def initialize(client, name, = {}) raise Error::InvalidDatabaseName.new unless name @client = client @name = name.to_s.freeze @options = .freeze end |
Instance Attribute Details
#client ⇒ Client (readonly)
Returns client The database client.
57 58 59 |
# File 'lib/mongo/database.rb', line 57 def client @client end |
#name ⇒ String (readonly)
Returns name The name of the database.
60 61 62 |
# File 'lib/mongo/database.rb', line 60 def name @name end |
#options ⇒ Hash (readonly)
Returns options The options.
63 64 65 |
# File 'lib/mongo/database.rb', line 63 def @options end |
Class Method Details
.create(client) ⇒ Database
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Create a database for the provided client, for use when we don’t want the client’s original database instance to be the same.
241 242 243 244 |
# File 'lib/mongo/database.rb', line 241 def self.create(client) database = Database.new(client, client.[:database], client.) client.instance_variable_set(:@database, database) end |
Instance Method Details
#==(other) ⇒ true, false
Check equality of the database object against another. Will simply check if the names are the same.
82 83 84 85 |
# File 'lib/mongo/database.rb', line 82 def ==(other) return false unless other.is_a?(Database) name == other.name end |
#[](collection_name, options = {}) ⇒ Mongo::Collection Also known as: collection
Get a collection in this database by the provided name.
98 99 100 |
# File 'lib/mongo/database.rb', line 98 def [](collection_name, = {}) Collection.new(self, collection_name, ) end |
#collection_names(options = {}) ⇒ Array<String>
Get all the names of the non system collections in the database.
111 112 113 |
# File 'lib/mongo/database.rb', line 111 def collection_names( = {}) View.new(self).collection_names() end |
#collections ⇒ Array<Mongo::Collection>
Get all the collections that belong to this database.
135 136 137 |
# File 'lib/mongo/database.rb', line 135 def collections collection_names.map { |name| collection(name) } end |
#command(operation, opts = {}) ⇒ Hash
Execute a command on the database.
150 151 152 153 154 155 156 157 158 159 |
# File 'lib/mongo/database.rb', line 150 def command(operation, opts = {}) preference = ServerSelector.get(client..merge(opts[:read])) if opts[:read] server = preference ? preference.select_server(cluster, false) : cluster.next_primary(false) Operation::Commands::Command.new({ :selector => operation, :db_name => name, :options => { :limit => -1 }, :read => preference }).execute(server.context) end |
#drop ⇒ Result
Drop the database and all its associated information.
169 170 171 |
# File 'lib/mongo/database.rb', line 169 def drop command(:dropDatabase => 1) end |
#fs(options = {}) ⇒ Grid::FSBucket
Get the Grid “filesystem” for this database.
212 213 214 |
# File 'lib/mongo/database.rb', line 212 def fs( = {}) Grid::FSBucket.new(self, ) end |
#inspect ⇒ String
Get a pretty printed string inspection for the database.
200 201 202 |
# File 'lib/mongo/database.rb', line 200 def inspect "#<Mongo::Database:0x#{object_id} name=#{name}>" end |
#list_collections ⇒ Array<Hash>
Get info on all the collections in the database.
123 124 125 |
# File 'lib/mongo/database.rb', line 123 def list_collections View.new(self).list_collections end |