This timeout should take into account both network latency and operation duration. For example, setting this timeout to 5 seconds will abort queries taking more than 5 seconds to execute on the server with Mongo::Error::SocketTimeoutError.
Note that even though by default there is no socket timeout set, the operating system may still time out read operations depending on its configuration. The keepalive settings are intended to detect broken network connections as opposed to aborting operations simply because they take a long time to execute. The number of seconds to wait for a socket connection to be established to a server.
Note that the timeout applies per lookup; due to DNS suffix search lists, multiple lookups may be performed as part of a single name resolution. The number of seconds to wait for a connection in the connection pool to become available. As of driver version 2. Specified as an option on a particular operation, the number of milliseconds to allow the operation to execute for on the server.
Not set by default. The number of milliseconds to wait for a write to be acknowledged by the number of servers specified in the write concern. Not set by default, which instructs the server to apply its default. All MongoDB server versions supported by the Ruby driver 2. For historical reasons, the Ruby option names pertaining to TLS configuration use the ssl rather than the tls prefix. The next major version of the Ruby driver 3. TLS must be explicitly requested on the client side when the deployment requires TLS connections - there is currently no automatic detection of whether the deployment requires TLS.
To request TLS connections, specify the following client options when constructing a Mongo::Client :. This can be accomplished via:.
When using the Ruby options, the client TLS certificate and the corresponding private key may be provided separately. For example, if the certificate is stored in client. The files containing both certificate and private key frequently have the. When both certificate and the private key are provided in the same file or string, both the certifcate and the key options must be utilized, as follows:. When using the URI option, the certificate and the key must be stored in a file and both must be stored in the same file.
Example usage:. This applies, for example, to slashes in the paths. It may be desirable to further configure TLS options in the driver, for example by enabling or disabling certain ciphers. Currently, the Ruby driver does not provide a way to do this when initializing a Mongo::Client. This should be done before creating any Mongo::Client instances. For example, in a Rails application this code could be placed in an initializer.
Every Proc in Mongo. It is possible to assign the entire array of hooks calling Mongo. It is recommended to use the Array push or Array unshift methods to add new hooks. It is also possible to remove hooks from Mongo. It is possible to use certificate chains for both the client and the server certificates. When using chains, the certificate authority parameter should be configured to contain the trusted root certificates only; the intermediate certificates, if any, should be provided in the server or client certificates by concatenating them after the leaf server and client certificates, respectively.
The Ruby driver performs strict X. More information about these flags can be found in this Stack Overflow question. By doing so, the intermediate certificates are elevated to trusted status and are themselves not verified against the actual CA root. More information on this issue is available in this mailing list post. By default, the driver will use the default system root certificate store as the trust anchor.
All certificates thus referenced will become trust anchors. Intermediate certificates must not be provided in files specified by the CA certificate options. Doing so would elevate the intermediate certificates to the status of root certificates, rather than verifying intermediate certificates against the root certificates.
If intermediate certificates need to be used, specify them as part of the client or server TLS certificate files. When a client is constructed with localhost as the host name, it will attempt an IPv4 connection only i. When a client is constructed with hostnames other than localhost , it will attempt both IPv4 and IPv6 connections depending on the addresses that the hostnames resolve to.
The driver respects the order in which getaddrinfo returns the addresses, and will attempt to connect to them sequentially. The first successful connection will be used. Where allowed by system configuration and the Ruby language runtime, the driver enables TCP keepalive and, for each of the keepalive parameters listed below, sets the value of the respective parameter to the specified value if the system value can be determined and is higher than the listed driver value:.
As of JRuby 9. When using JRuby, the driver will not be able to set the keepalive parameters and the system configuration will be in effect. To use lower values, or to change the parameters in environments like JRuby that do not expose the required APIs, please adjust the parameters at the system level as described in the MongoDB Diagnostics FAQ keepalive section. Mongo::Client instances have a connection pool per server that the client is connected to.
The pool creates connections on demand to support concurrent MongoDB operations issued by the application. There is no thread-affinity for connections. When a thread in the application begins an operation on MongoDB, it tries to retrieve a connection from the pool to send that operation on.
If there are some connections available in the pool, it checks out a connection from the pool and uses it for the operation. If all connections are in use and the pool has reached its maximum size, the thread waits for a connection to be returned to the pool by another thread. The number of seconds the thread will wait for a connection to become available is configurable. If this timeout is reached, a Timeout::Error is raised.
The default is 1 second. Prior to driver version 2. The purpose of these connections is to provide faster failover when the primary changes. Here is an example of estimating the number of connections a multi-threaded application will open: A client connected to a 3-node replica set opens 3 monitoring sockets.
The default configuration for a Mongo::Client works for most applications:. Create this client once for each process, and reuse it for all operations.
It is a common mistake to create a new client for each request, which is very inefficient and not what the client was designed for. When close is called on a client by any thread, all connections are closed:.
Note that when creating a client using the block syntax described above, the client is automatically closed after the block finishes executing. DateTime class in Ruby supports non-Gregorian calendars. When non-Gregorian DateTime instances are serialized, they are first converted to Gregorian calendar, and the respective date in the Gregorian calendar is stored in the database.
When Date instances are serialized, the time value used is midnight of the day that the Date refers to in UTC. Both MongoDB and Ruby provide facilities for working with regular expressions, but they use regular expression engines. The following subsections detail the differences between Ruby regular expressions and MongoDB regular expressions and describe how to work with both.
MongoDB server uses Perl-compatible regular expressions implemented using the PCRE library and Ruby regular expressions are implemented using the Onigmo regular expression engine , which is a fork of Oniguruma.
The two regular expression implementations generally provide equivalent functionality but have several important syntax differences, as described below. Unfortunately, there is no simple way to programmatically convert a PCRE regular expression into the equivalent Ruby regular expression, and there are currently no Ruby bindings for PCRE. The following sections provide workarounds and recommendations for authoring portable regular expressions.
Instances of this class can be created using the regular expression text as a string and optional PCRE modifiers:. Note that the s PCRE modifier was converted to the m Ruby modifier in the first example, and the last two examples were converted to the same regular expression even though the original BSON regular expressions had different meanings.
Methods of both classes return a BSON::Regexp::Raw instance that must be converted to a Ruby regular expression using the compile method as described above. BSON documents preserve the order of keys, because the documents are stored as lists of key-value pairs.
Hashes in Ruby also preserve key order; thus the order of keys specified in Ruby will be respected when serializing a hash to a BSON document, and when deserializing a BSON document into a hash the order of keys in the document will match the order of keys in the hash. Applications should refrain from generating such documents, because MongoDB server behavior is undefined when a BSON document contains duplicate keys.
Since in Ruby hashes cannot have duplicate keys, when serializing Ruby hashes to BSON documents no duplicate keys will be generated. Note that, since keys in BSON documents are always stored as strings, specifying the same key as as string and a symbol in Ruby only retains the most recent specification:. The following example creates a contacts collection with a validator that specifies that inserted or updated documents should match at least one of three following conditions:.
To add document validation criteria to an existing collection, use the collMod command. The example below demonstrates how to add a validation to the contacts collection, ensuring that all new documents must contain an age field which is a number.
Mongo::Client is used to connect to the MongoDB server. We specify the URL and the database name. The is the default port on which the MongoDB server listens. At the end, we close the connection. Generally, it is not recommended for applications to call close. The connections are expensive and are being reused. But since it is a one-off program and not a long running application which reuses connections, we do call the method.
Mongo::Error::NoServerAvailable is raised when we cannot connect to the database server.
0コメント