A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.mongodb.com/docs/languages/cpp/cpp-driver/current/connect/connection-targets/ below:

Choose a Connection Target - C++ Driver

In this guide, you can learn how to use a connection string and mongocxx::client object to connect to different types of MongoDB deployments.

To connect to a MongoDB deployment on Atlas, include the following elements in your connection string:

Then, pass your connection string to the mongocxx::uri constructor, and use the mongocxx::uri object to construct a mongocxx::client object.

When you connect to Atlas, we recommend using the Stable API client option to avoid breaking changes when Atlas upgrades to a new version of MongoDB Server. To learn more about the Stable API feature, see the Stable API page.

The following code shows how to use the C++ driver to connect to an Atlas cluster. The code also uses the server_api_opts option to specify a Stable API version.

#include <mongocxx/instance.hpp>#include <mongocxx/client.hpp>#include <mongocxx/uri.hpp>#include <bsoncxx/json.hpp>#include <mongocxx/exception/exception.hpp>using bsoncxx::builder::basic::kvp;using bsoncxx::builder::basic::make_document;int main(){    mongocxx::instance instance;        mongocxx::uri uri("<connection string>");        mongocxx::options::client client_options;    mongocxx::options::server_api server_api_options(mongocxx::options::server_api::version::k_version_1);    client_options.server_api_opts(server_api_options);    mongocxx::client client(uri, client_options);    try    {                auto admin = client["admin"];        auto command = make_document(kvp("ping", 1));        auto result = admin.run_command(command.view());        std::cout << bsoncxx::to_json(result) << "\n";        std::cout << "Pinged your deployment. You successfully connected to MongoDB!\n";    }    catch (const mongocxx::exception &e)    {        std::cerr << "An exception occurred: " << e.what() << "\n";        return EXIT_FAILURE;    }}

To connect to a local MongoDB deployment, use localhost as the hostname. By default, the mongod process runs on port 27017, though you can customize this for your deployment.

The following code shows how to use the C++ driver to connect to a local MongoDB deployment:

#include <mongocxx/instance.hpp>#include <mongocxx/uri.hpp>#include <mongocxx/client.hpp>int main(){    mongocxx::instance instance;    mongocxx::uri uri("mongodb://localhost:27017");    mongocxx::client client(uri);}

To connect to a replica set, specify the hostnames (or IP addresses) and port numbers of the replica set members in your connection string.

If you aren't able to provide a full list of hosts in the replica set, you can specify one or more of the hosts in the replica set and instruct the C++ driver to perform automatic discovery to find the others. To instruct the driver to perform automatic discovery, perform one of the following actions:

In the following example, the driver uses a sample connection URI to connect to the MongoDB replica set sampleRS, which is running on port 27017 of three different hosts, including host1:

#include <mongocxx/instance.hpp>#include <mongocxx/client.hpp>#include <mongocxx/uri.hpp>int main(){    mongocxx::instance instance;    mongocxx::uri uri("mongodb://host1:27017/?replicaSet=sampleRS");    mongocxx::client client(uri);}
Note Replica Set in Docker

When a replica set runs in Docker, it might expose only one MongoDB endpoint. In this case, the replica set is not discoverable. Specifying directConnection=false in your connection URI, or leaving this option unset, can prevent your application from connecting to it.

In a test or development environment, you can connect to the replica set by specifying directConnection=true. In a production environment, we recommend configuring the cluster to make each MongoDB instance accessible outside of the Docker virtual network.

To initialize a replica set, you must connect directly to a single member. To do so, set the directConnection connection option to true in the connection string. The following code example shows how to set this connection option:

#include <mongocxx/instance.hpp>#include <mongocxx/client.hpp>#include <mongocxx/uri.hpp>int main(){    mongocxx::instance instance;    mongocxx::uri uri("mongodb://<hostname>:<port>/?directConnection=true");    mongocxx::client client(uri);}

To learn more about the types used on this page, see the following API documentation:


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4