Class: Mongo::Database
- Inherits:
-
Object
- Object
- Mongo::Database
- Extended by:
- Forwardable
- Includes:
- Retryable
- 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'- COMMAND =
The “collection” that database commands operate against.
'$cmd'- DEFAULT_OPTIONS =
The default database options.
Options::Redacted.new(database: ADMIN).freeze
- NAME =
Deprecated.
Database name field constant.
'name'- DATABASES =
Databases constant.
'databases'- NAMESPACES =
The name of the collection that holds all the collection names.
'system.namespaces'
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.
-
#aggregate(pipeline, options = {}) ⇒ Collection::View::Aggregation
Perform an aggregation on the database.
-
#cluster ⇒ Mongo::Server
Get the primary server from the cluster.
-
#collection_names(options = {}) ⇒ Array<String>
Get all the names of the non-system collections in the database.
-
#collections(options = {}) ⇒ Array<Mongo::Collection>
Get all the non-system collections that belong to this database.
-
#command(operation, opts = {}) ⇒ Mongo::Operation::Result
Execute a command on the database.
-
#drop(options = {}) ⇒ 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(options = {}) ⇒ Array<Hash>
Get info on all the non-system collections in the database.
-
#operation_timeouts(opts) ⇒ Hash
private
Timeout_ms value set on the operation level (if any), and/or timeout_ms that is set on collection/database/client level (if any).
-
#read_command(operation, opts = {}) ⇒ Hash
private
Execute a read command on the database, retrying the read if necessary.
-
#timeout_ms ⇒ Integer | nil
private
Operation timeout that is for this database or for the corresponding client.
-
#users ⇒ View::User
Get the user view for this database.
-
#watch(pipeline = [], options = {}) ⇒ ChangeStream
Allows users to request that notifications are sent for all changes that occur in the client’s database.
Methods included from Retryable
#read_worker, #select_server, #with_overload_retry, #write_worker
Constructor Details
#initialize(client, name, options = {}) ⇒ Database
Instantiate a new database object.
365 366 367 368 369 370 371 372 373 374 |
# File 'lib/mongo/database.rb', line 365 def initialize(client, name, = {}) raise Error::InvalidDatabaseName.new unless name if Lint.enabled? && !(name.is_a?(String) || name.is_a?(Symbol)) raise "Database name must be a string or a symbol: #{name}" end @client = client @name = name.to_s.freeze @options = .freeze end |
Instance Attribute Details
#client ⇒ Client (readonly)
Returns client The database client.
60 61 62 |
# File 'lib/mongo/database.rb', line 60 def client @client end |
#name ⇒ String (readonly)
Returns name The name of the database.
63 64 65 |
# File 'lib/mongo/database.rb', line 63 def name @name end |
#options ⇒ Hash (readonly)
Returns options The options.
66 67 68 |
# File 'lib/mongo/database.rb', line 66 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.
546 547 548 549 |
# File 'lib/mongo/database.rb', line 546 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.
93 94 95 96 97 |
# File 'lib/mongo/database.rb', line 93 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.
110 111 112 113 114 115 116 117 |
# File 'lib/mongo/database.rb', line 110 def [](collection_name, = {}) if [:server_api] raise ArgumentError, 'The :server_api option cannot be specified for collection objects. It can only be specified on Client level' end Collection.new(self, collection_name, ) end |
#aggregate(pipeline, options = {}) ⇒ Collection::View::Aggregation
Perform an aggregation on the database.
454 455 456 |
# File 'lib/mongo/database.rb', line 454 def aggregate(pipeline, = {}) View.new(self, ).aggregate(pipeline, ) end |
#cluster ⇒ Mongo::Server
Returns Get the primary server from the cluster.
79 80 |
# File 'lib/mongo/database.rb', line 79 def_delegators :cluster, :next_primary |
#collection_names(options = {}) ⇒ Array<String>
The set of returned collection names depends on the version of MongoDB server that fulfills the request.
Get all the names of the non-system collections in the database.
See https://mongodb.com/docs/manual/reference/command/listCollections/
for more information and usage.
144 145 146 |
# File 'lib/mongo/database.rb', line 144 def collection_names( = {}) View.new(self, ).collection_names() end |
#collections(options = {}) ⇒ Array<Mongo::Collection>
The set of returned collections depends on the version of MongoDB server that fulfills the request.
Get all the non-system collections that belong to this database.
See https://mongodb.com/docs/manual/reference/command/listCollections/
for more information and usage.
205 206 207 |
# File 'lib/mongo/database.rb', line 205 def collections( = {}) collection_names().map { |name| collection(name) } end |
#command(operation, opts = {}) ⇒ Mongo::Operation::Result
Execute a command on the database.
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/mongo/database.rb', line 231 def command(operation, opts = {}) opts = opts.dup execution_opts = opts.delete(:execution_options) || {} txn_read_pref = (opts[:session].txn_read_preference if opts[:session] && opts[:session].in_transaction?) txn_read_pref ||= opts[:read] || ServerSelector::PRIMARY Lint.validate_underscore_read_preference(txn_read_pref) selector = ServerSelector.get(txn_read_pref) client.with_session(opts) do |session| context = Operation::Context.new( client: client, session: session, operation_timeouts: operation_timeouts(opts) ) op = Operation::Command.new( selector: operation, db_name: name, read: selector, session: session ) retry_enabled = client.[:retry_reads] != false && client.[:retry_writes] != false with_overload_retry(context: context, retry_enabled: retry_enabled) do server = selector.select_server(cluster, nil, session) op.execute(server, context: context, options: execution_opts) end end end |
#drop(options = {}) ⇒ Result
Drop the database and all its associated information.
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/mongo/database.rb', line 325 def drop( = {}) operation = { dropDatabase: 1 } client.with_session() do |session| write_concern = if [:write_concern] WriteConcern.get([:write_concern]) else self.write_concern end Operation::DropDatabase.new({ selector: operation, db_name: name, write_concern: write_concern, session: session }).execute( next_primary(nil, session), context: Operation::Context.new( client: client, session: session, operation_timeouts: operation_timeouts() ) ) end end |
#fs(options = {}) ⇒ Grid::FSBucket
Get the Grid “filesystem” for this database.
408 409 410 |
# File 'lib/mongo/database.rb', line 408 def fs( = {}) Grid::FSBucket.new(self, ) end |
#inspect ⇒ String
Get a pretty printed string inspection for the database.
384 385 386 |
# File 'lib/mongo/database.rb', line 384 def inspect "#<Mongo::Database:0x#{object_id} name=#{name}>" end |
#list_collections(options = {}) ⇒ Array<Hash>
The set of collections returned, and the schema of the information hash per collection, depends on the MongoDB server version that fulfills the request.
Get info on all the non-system collections in the database.
See https://mongodb.com/docs/manual/reference/command/listCollections/
for more information and usage.
177 178 179 |
# File 'lib/mongo/database.rb', line 177 def list_collections( = {}) View.new(self, ).list_collections() end |
#operation_timeouts(opts) ⇒ Hash
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.
Returns timeout_ms value set on the operation level (if any), and/or timeout_ms that is set on collection/database/client level (if any).
563 564 565 566 567 568 569 570 571 572 |
# File 'lib/mongo/database.rb', line 563 def operation_timeouts(opts) # TODO: We should re-evaluate if we need two timeouts separately. {}.tap do |result| if opts[:timeout_ms].nil? result[:inherited_timeout_ms] = timeout_ms else result[:operation_timeout_ms] = opts.delete(:timeout_ms) end end end |
#read_command(operation, opts = {}) ⇒ Hash
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.
Execute a read command on the database, retrying the read if necessary.
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/mongo/database.rb', line 280 def read_command(operation, opts = {}) txn_read_pref = (opts[:session].txn_read_preference if opts[:session] && opts[:session].in_transaction?) txn_read_pref ||= opts[:read] || ServerSelector::PRIMARY Lint.validate_underscore_read_preference(txn_read_pref) preference = ServerSelector.get(txn_read_pref) client.with_session(opts) do |session| context = Operation::Context.new( client: client, session: session, operation_timeouts: operation_timeouts(opts) ) operation = Operation::Command.new( selector: operation.dup, db_name: name, read: preference, session: session, comment: opts[:comment] ) op_name = opts[:op_name] || 'command' tracer.trace_operation(operation, context, op_name: op_name) do read_with_retry(session, preference, context) do |server| operation.execute(server, context: context) end end end end |
#timeout_ms ⇒ Integer | nil
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.
Returns Operation timeout that is for this database or for the corresponding client.
555 556 557 |
# File 'lib/mongo/database.rb', line 555 def timeout_ms [:timeout_ms] || client.timeout_ms end |
#users ⇒ View::User
Get the user view for this database.
420 421 422 |
# File 'lib/mongo/database.rb', line 420 def users Auth::User::View.new(self) end |
#watch(pipeline = [], options = {}) ⇒ ChangeStream
A change stream only allows ‘majority’ read concern.
This helper method is preferable to running a raw aggregation with a $changeStream stage, for the purpose of supporting resumability.
Allows users to request that notifications are sent for all changes that occur in the client’s database.
521 522 523 524 525 526 527 528 529 530 531 |
# File 'lib/mongo/database.rb', line 521 def watch(pipeline = [], = {}) = .dup [:cursor_type] = :tailable_await if [:max_await_time_ms] Mongo::Collection::View::ChangeStream.new( Mongo::Collection::View.new(collection("#{COMMAND}.aggregate"), {}, ), pipeline, Mongo::Collection::View::ChangeStream::DATABASE, ) end |