Docs Menu
Docs Home
/ / /
C++ Driver
/

Specify Connection Options

On this page

  • Overview
  • Set Connection Options
  • Using the Connection URI
  • Using a mongocxx::options::client Object
  • Read Connection Options
  • Connection URI Options
  • Replica Set Options
  • Connection Options
  • Connection Pool Options
  • Write Concern Options
  • Read Concern Options
  • Read Preference Options
  • Authentication Options
  • Server Selection and Discovery Options
  • Miscellaneous Configuration

This page describes the MongoDB connection and authentication options available in the C++ driver.

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.

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:

#include <mongocxx/uri.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
int main()
{
mongocxx::instance instance;
mongocxx::uri uri("mongodb://<hostname>:<port>/?tls=true&tlsCertificateKeyFile=path/to/file.pem");
mongocxx::client client(uri);
}

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:

#include <mongocxx/uri.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
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);
}

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:

#include <mongocxx/uri.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
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.

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.

Connection URI Option
mongocxx::uri Member

directConnection

Tip

You can set most TLS options by using the client_options parameter. See the mongocxx::options::tls API documentation for more information.

Connection URI Option
mongocxx::uri Member

N/A

N/A

Connection URI Option
mongocxx::uri Member
Connection URI Option
mongocxx::uri Member

N/A

N/A

Back

Choose a Connection Target