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.
[ :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, :read, :read_retry_interval, :replica_set, :server_selection_timeout, :socket_timeout, :ssl, :ssl_ca_cert, :ssl_cert, :ssl_key, :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.
197 198 199 200 201 202 203 204 205 |
# File 'lib/mongo/client.rb', line 197 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.
68 69 70 |
# File 'lib/mongo/client.rb', line 68 def cluster @cluster end |
#database ⇒ Mongo::Database (readonly)
Returns database The database the client is operating on.
71 72 73 |
# File 'lib/mongo/client.rb', line 71 def database @database end |
#options ⇒ Hash (readonly)
Returns options The configuration options.
74 75 76 |
# File 'lib/mongo/client.rb', line 74 def end |
Instance Method Details
#==(other) ⇒ true, false Also known as: eql?
Determine if this client is equivalent to another object.
92 93 94 95 |
# File 'lib/mongo/client.rb', line 92 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.
109 110 111 |
# File 'lib/mongo/client.rb', line 109 def [](collection_name, = {}) database[collection_name, ] end |
#close ⇒ true
Close all connections.
293 294 295 |
# File 'lib/mongo/client.rb', line 293 def close @cluster.disconnect! and true end |
#database_names ⇒ Array<String>
Get the names of all databases.
317 318 319 |
# File 'lib/mongo/client.rb', line 317 def database_names list_databases.collect{ |info| info[Database::NAME] } end |
#hash ⇒ Integer
Get the hash value of the client.
121 122 123 |
# File 'lib/mongo/client.rb', line 121 def hash [cluster, ].hash end |
#inspect ⇒ String
Get an inspection of the client as a string.
215 216 217 |
# File 'lib/mongo/client.rb', line 215 def inspect "#<Mongo::Client:0x#{object_id} cluster=#{cluster.addresses.join(', ')}>" end |
#list_databases ⇒ Array<Hash>
Get info for each database.
329 330 331 |
# File 'lib/mongo/client.rb', line 329 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.
228 229 230 |
# File 'lib/mongo/client.rb', line 228 def read_preference @read_preference ||= ServerSelector.get(Options::Redacted.new([:read] || {}).merge()) end |
#reconnect ⇒ true
Reconnect the client.
305 306 307 |
# File 'lib/mongo/client.rb', line 305 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.
243 244 245 |
# File 'lib/mongo/client.rb', line 243 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.
259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/mongo/client.rb', line 259 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.
281 282 283 |
# File 'lib/mongo/client.rb', line 281 def write_concern @write_concern ||= WriteConcern.get([:write]) end |