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'.freeze
- COMMAND =
The “collection” that database commands operate against.
'$cmd'.freeze
- DEFAULT_OPTIONS =
The default database options.
Options::Redacted.new(:database => ADMIN).freeze
- NAME =
Deprecated.
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.
-
#aggregate(pipeline, options = {}) ⇒ 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.
-
#read_command(operation, opts = {}) ⇒ Hash
private
Execute a read command on the database, retrying the read if necessary.
-
#users ⇒ View::User
Get the user view for this database.
-
#watch(pipeline = [], options = {}) ⇒ ChangeStream
As of version 3.6 of the MongoDB server, a “$changeStream“ pipeline stage is supported in the aggregation framework.
Methods included from Retryable
#legacy_write_with_retry, #nro_write_with_retry, #read_with_one_retry, #read_with_retry, #read_with_retry_cursor, #write_with_retry
Constructor Details
#initialize(client, name, options = {}) ⇒ Database
Instantiate a new database object.
294 295 296 297 298 299 300 301 302 |
# File 'lib/mongo/database.rb', line 294 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 = .freeze end |
Instance Attribute Details
#client ⇒ Client (readonly)
Returns client The database client.
59 60 61 |
# File 'lib/mongo/database.rb', line 59 def client @client end |
#name ⇒ String (readonly)
Returns name The name of the database.
62 63 64 |
# File 'lib/mongo/database.rb', line 62 def name @name end |
#options ⇒ Hash (readonly)
Returns options The options.
65 66 67 |
# File 'lib/mongo/database.rb', line 65 def 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.
426 427 428 429 |
# File 'lib/mongo/database.rb', line 426 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.
90 91 92 93 |
# File 'lib/mongo/database.rb', line 90 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.
106 107 108 |
# File 'lib/mongo/database.rb', line 106 def [](collection_name, = {}) Collection.new(self, collection_name, ) end |
#aggregate(pipeline, options = {}) ⇒ Aggregation
Perform an aggregation on the database.
368 369 370 |
# File 'lib/mongo/database.rb', line 368 def aggregate(pipeline, = {}) View.new(self).aggregate(pipeline, ) end |
#cluster ⇒ Mongo::Server
Returns Get the primary server from the cluster.
76 77 |
# File 'lib/mongo/database.rb', line 76 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.
129 130 131 |
# File 'lib/mongo/database.rb', line 129 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.
178 179 180 |
# File 'lib/mongo/database.rb', line 178 def collections( = {}) collection_names().map { |name| collection(name) } end |
#command(operation, opts = {}) ⇒ Mongo::Operation::Result
Execute a command on the database.
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/mongo/database.rb', line 200 def command(operation, opts = {}) opts = opts.dup execution_opts = opts.delete(:execution_options) || {} txn_read_pref = if opts[:session] && opts[:session].in_transaction? opts[:session].txn_read_preference else nil end txn_read_pref ||= opts[:read] || ServerSelector::PRIMARY Lint.validate_underscore_read_preference(txn_read_pref) selector = ServerSelector.get(txn_read_pref) client.send(:with_session, opts) do |session| server = selector.select_server(cluster, nil, session) op = Operation::Command.new( :selector => operation.dup, :db_name => name, :read => selector, :session => session ) op.execute(server, client: client, options: execution_opts) end end |
#drop(options = {}) ⇒ Result
Drop the database and all its associated information.
270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/mongo/database.rb', line 270 def drop( = {}) operation = { :dropDatabase => 1 } client.send(:with_session, ) do |session| Operation::DropDatabase.new({ selector: operation, db_name: name, write_concern: write_concern, session: session }).execute(next_primary(nil, session), client: client) end end |
#fs(options = {}) ⇒ Grid::FSBucket
Get the Grid “filesystem” for this database.
324 325 326 |
# File 'lib/mongo/database.rb', line 324 def fs( = {}) Grid::FSBucket.new(self, ) end |
#inspect ⇒ String
Get a pretty printed string inspection for the database.
312 313 314 |
# File 'lib/mongo/database.rb', line 312 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.
156 157 158 |
# File 'lib/mongo/database.rb', line 156 def list_collections( = {}) View.new(self).list_collections() 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.
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/mongo/database.rb', line 236 def read_command(operation, opts = {}) txn_read_pref = if opts[:session] && opts[:session].in_transaction? opts[:session].txn_read_preference else nil end txn_read_pref ||= opts[:read] || ServerSelector::PRIMARY Lint.validate_underscore_read_preference(txn_read_pref) preference = ServerSelector.get(txn_read_pref) client.send(:with_session, opts) do |session| read_with_retry(session, preference) do |server| Operation::Command.new({ :selector => operation.dup, :db_name => name, :read => preference, :session => session }).execute(server, client: client) end end end |
#users ⇒ View::User
Get the user view for this database.
336 337 338 |
# File 'lib/mongo/database.rb', line 336 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.
As of version 3.6 of the MongoDB server, a “$changeStream“ pipeline stage is supported in the aggregation framework. As of version 4.0, this stage allows users to request that notifications are sent for all changes that occur in the client’s database.
405 406 407 408 409 410 411 |
# File 'lib/mongo/database.rb', line 405 def watch(pipeline = [], = {}) Mongo::Collection::View::ChangeStream.new( Mongo::Collection::View.new(collection("#{COMMAND}.aggregate")), pipeline, Mongo::Collection::View::ChangeStream::DATABASE, ) end |