Class: Mongo::Client
- Inherits:
-
Object
- Object
- Mongo::Client
- Extended by:
- Forwardable
- Includes:
- Loggable
- Defined in:
- lib/mongo/client.rb
Overview
The client is the entry point to the driver and is the main object that will be interacted with.
Constant Summary collapse
- CRUD_OPTIONS =
The options that do not affect the behaviour of a cluster and its subcomponents.
[ :database, :read, :write ].freeze
- VALID_OPTIONS =
Valid client options.
[ :app_name, :auth_mech, :auth_mech_properties, :auth_source, :connect, :connect_timeout, :compressors, :database, :heartbeat_frequency, :id_generator, :local_threshold, :logger, :max_idle_time, :max_pool_size, :max_read_retries, :min_pool_size, :monitoring, :password, :platform, :read, :read_concern, :read_retry_interval, :replica_set, :retry_writes, :server_selection_timeout, :socket_timeout, :ssl, :ssl_ca_cert, :ssl_ca_cert_string, :ssl_ca_cert_object, :ssl_cert, :ssl_cert_string, :ssl_cert_object, :ssl_key, :ssl_key_string, :ssl_key_object, :ssl_key_pass_phrase, :ssl_verify, :truncate_logs, :user, :wait_queue_timeout, :write, :zlib_compression_level ].freeze
- VALID_COMPRESSORS =
The compression algorithms supported by the driver.
[ Mongo::Protocol::Compressed::ZLIB ].freeze
Constants included from Loggable
Instance Attribute Summary collapse
-
#cluster ⇒ Mongo::Cluster
readonly
Cluster The cluster of servers for the client.
-
#database ⇒ Mongo::Database
readonly
Database The database the client is operating on.
-
#options ⇒ Hash
readonly
Options The configuration options.
Instance Method Summary collapse
-
#==(other) ⇒ true, false
(also: #eql?)
Determine if this client is equivalent to another object.
-
#[](collection_name, options = {}) ⇒ Mongo::Collection
Get a collection object for the provided collection name.
-
#close ⇒ true
Close all connections.
-
#database_names(filter = {}, opts = {}) ⇒ Array<String>
Get the names of all databases.
-
#hash ⇒ Integer
Get the hash value of the client.
-
#initialize(addresses_or_uri, options = Options::Redacted.new) {|_self| ... } ⇒ Client
constructor
Instantiate a new driver client.
-
#inspect ⇒ String
Get an inspection of the client as a string.
-
#list_databases(filter = {}, name_only = false, opts = {}) ⇒ Array<Hash>
Get info for each database.
-
#list_mongo_databases(filter = {}, opts = {}) ⇒ Array<Mongo::Database>
Returns a list of Mongo::Database objects.
-
#read_concern ⇒ Hash
Get the read concern for this client.
-
#read_preference ⇒ BSON::Document
Get the read preference from the options passed to the client.
-
#reconnect ⇒ true
Reconnect the client.
-
#server_selector ⇒ Mongo::ServerSelector
Get the server selector.
-
#start_session(options = {}) ⇒ Session
Start a session.
-
#use(name) ⇒ Mongo::Client
Use the database with the provided name.
-
#watch(pipeline = [], options = {}) ⇒ ChangeStream
As of version 3.6 of the MongoDB server, a “$changeStream“ pipeline stage is supported in the aggregation framework.
-
#with(new_options = Options::Redacted.new) ⇒ Mongo::Client
Provides a new client with the passed options merged over the existing options of this client.
-
#write_concern ⇒ Mongo::WriteConcern
Get the write concern for this client.
Methods included from Loggable
#log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger
Constructor Details
#initialize(addresses_or_uri, options = Options::Redacted.new) {|_self| ... } ⇒ Client
Instantiate a new driver client.
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/mongo/client.rb', line 267 def initialize(addresses_or_uri, = Options::Redacted.new) Mongo::Lint.validate_underscore_read_preference([:read]) if addresses_or_uri.is_a?(::String) uri = URI.get(addresses_or_uri, ) addresses = uri.servers = uri..merge() else addresses = addresses_or_uri end = .dup # Special handling for sdam_proc as it is only used during client # construction sdam_proc = .delete(:sdam_proc) = (Database::DEFAULT_OPTIONS.merge()).freeze @database = Database.new(self, [:database], ) monitoring = Monitoring.new() if sdam_proc @cluster = Cluster.new([], monitoring, ) sdam_proc.call(self) end @cluster = Cluster.new(addresses, monitoring, ) yield(self) if block_given? end |
Instance Attribute Details
#cluster ⇒ Mongo::Cluster (readonly)
Returns cluster The cluster of servers for the client.
86 87 88 |
# File 'lib/mongo/client.rb', line 86 def cluster @cluster end |
#database ⇒ Mongo::Database (readonly)
Returns database The database the client is operating on.
89 90 91 |
# File 'lib/mongo/client.rb', line 89 def database @database end |
#options ⇒ Hash (readonly)
Returns options The configuration options.
92 93 94 |
# File 'lib/mongo/client.rb', line 92 def end |
Instance Method Details
#==(other) ⇒ true, false Also known as: eql?
Determine if this client is equivalent to another object.
115 116 117 118 |
# File 'lib/mongo/client.rb', line 115 def ==(other) return false unless other.is_a?(Client) cluster == other.cluster && == other. end |
#[](collection_name, options = {}) ⇒ Mongo::Collection
Get a collection object for the provided collection name.
132 133 134 |
# File 'lib/mongo/client.rb', line 132 def [](collection_name, = {}) database[collection_name, ] end |
#close ⇒ true
Close all connections.
409 410 411 |
# File 'lib/mongo/client.rb', line 409 def close @cluster.disconnect! and true end |
#database_names(filter = {}, opts = {}) ⇒ Array<String>
Get the names of all databases.
442 443 444 |
# File 'lib/mongo/client.rb', line 442 def database_names(filter = {}, opts = {}) list_databases(filter, true, opts).collect{ |info| info[Database::NAME] } end |
#hash ⇒ Integer
Get the hash value of the client.
144 145 146 |
# File 'lib/mongo/client.rb', line 144 def hash [cluster, ].hash end |
#inspect ⇒ String
Get an inspection of the client as a string.
299 300 301 |
# File 'lib/mongo/client.rb', line 299 def inspect "#<Mongo::Client:0x#{object_id} cluster=#{cluster.addresses.join(', ')}>" end |
#list_databases(filter = {}, name_only = false, opts = {}) ⇒ Array<Hash>
Get info for each database.
458 459 460 461 462 463 |
# File 'lib/mongo/client.rb', line 458 def list_databases(filter = {}, name_only = false, opts = {}) cmd = { listDatabases: 1 } cmd[:nameOnly] = !!name_only cmd[:filter] = filter unless filter.empty? use(Database::ADMIN).command(cmd, opts).first[Database::DATABASES] end |
#list_mongo_databases(filter = {}, opts = {}) ⇒ Array<Mongo::Database>
Returns a list of Mongo::Database objects.
476 477 478 479 480 |
# File 'lib/mongo/client.rb', line 476 def list_mongo_databases(filter = {}, opts = {}) database_names(filter, opts).collect do |name| Database.new(self, name, ) end end |
#read_concern ⇒ Hash
Get the read concern for this client.
383 384 385 |
# File 'lib/mongo/client.rb', line 383 def read_concern @read_concern ||= [:read_concern] end |
#read_preference ⇒ BSON::Document
Get the read preference from the options passed to the client.
331 332 333 |
# File 'lib/mongo/client.rb', line 331 def read_preference @read_preference ||= [:read] end |
#reconnect ⇒ true
Reconnect the client.
421 422 423 424 425 426 427 428 429 |
# File 'lib/mongo/client.rb', line 421 def reconnect addresses = cluster.addresses.map(&:to_s) monitoring = cluster.monitoring @cluster.disconnect! rescue nil @cluster = Cluster.new(addresses, monitoring, ) true end |
#server_selector ⇒ Mongo::ServerSelector
Get the server selector. It either uses the read preference defined in the client options or defaults to a Primary server selector.
313 314 315 |
# File 'lib/mongo/client.rb', line 313 def server_selector @server_selector ||= ServerSelector.get(read_preference || ServerSelector::PRIMARY) end |
#start_session(options = {}) ⇒ Session
A Session cannot be used by multiple threads at once; session objects are not thread-safe.
Start a session.
495 496 497 498 |
# File 'lib/mongo/client.rb', line 495 def start_session( = {}) cluster.send(:get_session, self, .merge(implicit: false)) || (raise Error::InvalidSession.new(Session::SESSIONS_NOT_SUPPORTED)) end |
#use(name) ⇒ Mongo::Client
Use the database with the provided name. This will switch the current database the client is operating on.
346 347 348 |
# File 'lib/mongo/client.rb', line 346 def use(name) with(database: name) 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 cluster.
533 534 535 536 537 538 539 540 541 |
# File 'lib/mongo/client.rb', line 533 def watch(pipeline = [], = {}) return use(Database::ADMIN).watch(pipeline, ) unless database.name == Database::ADMIN Mongo::Collection::View::ChangeStream.new( Mongo::Collection::View.new(self["#{Database::COMMAND}.aggregate"]), pipeline, Mongo::Collection::View::ChangeStream::CLUSTER, ) end |
#with(new_options = Options::Redacted.new) ⇒ Mongo::Client
Provides a new client with the passed options merged over the existing options of this client. Useful for one-offs to change specific options without altering the original client.
362 363 364 365 366 367 368 369 370 371 372 373 |
# File 'lib/mongo/client.rb', line 362 def with( = Options::Redacted.new) clone.tap do |client| opts = () client..update(opts) Database.create(client) # We can't use the same cluster if some options that would affect it # have changed. if (opts) Cluster.create(client) end end end |
#write_concern ⇒ Mongo::WriteConcern
Get the write concern for this client. If no option was provided, then a default single server acknowledgement will be used.
397 398 399 |
# File 'lib/mongo/client.rb', line 397 def write_concern @write_concern ||= WriteConcern.get([:write]) end |