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, :database, :heartbeat_frequency, :id_generator, :local_threshold, :logger, :max_pool_size, :max_read_retries, :min_pool_size, :monitoring, :password, :platform, :read, :read_retry_interval, :replica_set, :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 ].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 ⇒ 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 ⇒ Array<Hash>
Get info for each database.
-
#read_preference ⇒ Object
Get the read preference from the options passed to the client.
-
#reconnect ⇒ true
Reconnect the client.
-
#use(name) ⇒ Mongo::Client
Use the database with the provided name.
-
#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.
230 231 232 233 234 235 236 237 238 |
# File 'lib/mongo/client.rb', line 230 def initialize(addresses_or_uri, = Options::Redacted.new) @monitoring = Monitoring.new() if addresses_or_uri.is_a?(::String) create_from_uri(addresses_or_uri, ()) else create_from_addresses(addresses_or_uri, ()) end yield(self) if block_given? end |
Instance Attribute Details
#cluster ⇒ Mongo::Cluster (readonly)
Returns cluster The cluster of servers for the client.
76 77 78 |
# File 'lib/mongo/client.rb', line 76 def cluster @cluster end |
#database ⇒ Mongo::Database (readonly)
Returns database The database the client is operating on.
79 80 81 |
# File 'lib/mongo/client.rb', line 79 def database @database end |
#options ⇒ Hash (readonly)
Returns options The configuration options.
82 83 84 |
# File 'lib/mongo/client.rb', line 82 def @options end |
Instance Method Details
#==(other) ⇒ true, false Also known as: eql?
Determine if this client is equivalent to another object.
100 101 102 103 |
# File 'lib/mongo/client.rb', line 100 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.
117 118 119 |
# File 'lib/mongo/client.rb', line 117 def [](collection_name, = {}) database[collection_name, ] end |
#close ⇒ true
Close all connections.
326 327 328 |
# File 'lib/mongo/client.rb', line 326 def close @cluster.disconnect! and true end |
#database_names ⇒ Array<String>
Get the names of all databases.
350 351 352 |
# File 'lib/mongo/client.rb', line 350 def database_names list_databases.collect{ |info| info[Database::NAME] } end |
#hash ⇒ Integer
Get the hash value of the client.
129 130 131 |
# File 'lib/mongo/client.rb', line 129 def hash [cluster, ].hash end |
#inspect ⇒ String
Get an inspection of the client as a string.
248 249 250 |
# File 'lib/mongo/client.rb', line 248 def inspect "#<Mongo::Client:0x#{object_id} cluster=#{cluster.addresses.join(', ')}>" end |
#list_databases ⇒ Array<Hash>
Get info for each database.
362 363 364 |
# File 'lib/mongo/client.rb', line 362 def list_databases use(Database::ADMIN).command(listDatabases: 1).first[Database::DATABASES] end |
#read_preference ⇒ Object
Get the read preference from the options passed to the client.
261 262 263 |
# File 'lib/mongo/client.rb', line 261 def read_preference @read_preference ||= ServerSelector.get([:read] || ServerSelector::PRIMARY) end |
#reconnect ⇒ true
Reconnect the client.
338 339 340 |
# File 'lib/mongo/client.rb', line 338 def reconnect @cluster.reconnect! and true end |
#use(name) ⇒ Mongo::Client
Use the database with the provided name. This will switch the current database the client is operating on.
276 277 278 |
# File 'lib/mongo/client.rb', line 276 def use(name) with(database: name) 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.
292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/mongo/client.rb', line 292 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.
314 315 316 |
# File 'lib/mongo/client.rb', line 314 def write_concern @write_concern ||= WriteConcern.get([:write]) end |