Overview
This page describes the MongoDB connection and authentication options available in the C++ driver.
Set Connection Options
You can configure your connection by specifying options in the connection URI or by
passing an instance of the mongocxx::options::client class as the client_options
parameter to the mongocxx::client constructor.
Note
You can specify some connection options only in the connection URI, and others only
in an the client_options parameter.
You might need to combine these methods to specify all the options that you need.
Using the Connection URI
When you construct a mongocxx::client object, you can pass in a mongocxx::uri
object that represents your connection URI. This connection URI can include
connection options as <name>=<value> pairs. In the following example,
the connection URI contains the tls option with a value of true and the
tlsCertificateKeyFile option with a value of path/to/file.pem:
int main() { mongocxx::instance instance; mongocxx::uri uri("mongodb://<hostname>:<port>/?tls=true&tlsCertificateKeyFile=path/to/file.pem"); mongocxx::client client(uri); }
Using a mongocxx::options::client Object
The mongocxx::client constructor includes a client_options parameter that
accepts an instance of the mongocxx::options::client class. You can specify
certain options in the client_options parameter instead of including them in your
connection URI.
The following example shows how to use the client_options parameter
to set connection options:
int main() { mongocxx::instance instance; mongocxx::options::client client_options; mongocxx::options::tls tls_options; tls_options.pem_file("/path/to/file.pem"); client_options.tls_opts(tls_options); mongocxx::uri uri("mongodb://<hostname>:<port>/?tls=true"); mongocxx::client client(uri, client_options); }
Read Connection Options
After constructing a mongocxx::client object, you can read the values of certain
connection options by using properties of the
mongocxx::uri object.
The following example shows how to read the value of the tls connection options
by using the tls() property:
int main() { mongocxx::instance instance; mongocxx::uri uri("mongodb://<hostname>:<port>/?tls=true"); mongocxx::client client(uri); auto is_tls_enabled = uri.tls(); }
The following sections show the corresponding mongocxx::uri property for
each connection option that supports it.
Connection URI Options
The following sections describe the connection options that you can set in the connection
URI passed to the C++ driver. Each connection option links to
the MongoDB Server manual and to its corresponding mongocxx::uri
property, if supported.
Replica Set Options
Connection URI Option | mongocxx::uri Member |
|---|---|
Connection Options
TLS Options
Connection URI Option | mongocxx::uri Member |
|---|---|
Tip
You can set most TLS options by using the client_options parameter. See the
mongocxx::options::tls
API documentation for more information.
Timeout Options
Connection URI Option | mongocxx::uri Member |
|---|---|
Compression Options
Connection URI Option | mongocxx::uri Member |
|---|---|
Connection Pool Options
Connection URI Option | mongocxx::uri Member |
|---|---|
Write Concern Options
Connection URI Option | mongocxx::uri Member |
|---|---|
N/A | |
N/A |
Read Concern Options
Connection URI Option | mongocxx::uri Member |
|---|---|
Read Preference Options
Connection URI Option | mongocxx::uri Member |
|---|---|
N/A | |
N/A |
Authentication Options
Connection URI Option | mongocxx::uri Member |
|---|---|
Server Selection and Discovery Options
Miscellaneous Configuration
Connection URI Option | mongocxx::uri Member |
|---|---|
N/A | |