-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathconnection-targets.txt
121 lines (87 loc) · 3.82 KB
/
connection-targets.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
.. _cpp-connection-targets:
==========================
Choose a Connection Target
==========================
.. facet::
:name: genre
:values: reference
.. meta::
:keywords: connection string, URI, server, settings, client, stable api
.. contents:: On this page
:local:
:backlinks: none
:depth: 2
:class: singlecol
Overview
--------
In this guide, you can learn how to use a connection string and ``mongocxx::client`` object
to connect to different types of MongoDB deployments.
.. _cpp-connection-atlas:
Atlas
-----
To connect to a MongoDB deployment on Atlas, include the following elements
in your connection string:
- URI of your Atlas cluster
- MongoDB database username
- MongoDB database password
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 {+mdb-server+}.
To learn more about the {+stable-api+} feature, see the :ref:`{+stable-api+} page
<cpp-stable-api>`.
The following code shows how to use the {+driver-short+} to connect to an Atlas cluster.
The code also uses the ``server_api_opts`` option to specify a {+stable-api+} version.
.. literalinclude:: /includes/connect/atlas.cpp
:copyable: true
:language: cpp
.. tip::
Follow the :atlas:`Atlas driver connection guide </driver-connection>`
to retrieve your connection string.
.. _cpp-connection-local:
Local Deployments
-----------------
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 {+driver-short+} to connect to a local MongoDB
deployment:
.. literalinclude:: /includes/connect/client.cpp
:language: cpp
:copyable: true
.. _cpp-connection-replica-set:
Replica Sets
------------
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 {+driver-short+} to
perform automatic discovery to find the others. To instruct the driver to perform
automatic discovery, perform one of the following actions:
- Specify the name of the replica set as the value of the ``replicaSet`` parameter.
- Specify ``false`` as the value of the ``directConnection`` parameter.
- Specify more than one host in the replica set.
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``:
.. literalinclude:: /includes/connect/replica-set.cpp
:language: cpp
:copyable: true
Initialization
~~~~~~~~~~~~~~
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:
.. literalinclude:: /includes/connect/direct-connection.cpp
:language: cpp
:copyable: true
API Documentation
-----------------
To learn more about the types used on this page,
see the following API documentation:
- `mongocxx::client <{+api+}/classmongocxx_1_1v__noabi_1_1client.html>`__
- `mongocxx::uri <{+api+}/classmongocxx_1_1v__noabi_1_1uri.html>`__
- `mongocxx::instance <{+api+}/classmongocxx_1_1v__noabi_1_1instance.html>`__
- `mongocxx::options::client <{+api+}/classmongocxx_1_1v__noabi_1_1options_1_1client.html>`__
- `mongocxx::options::server_api <{+api+}/classmongocxx_1_1v__noabi_1_1options_1_1server__api.html>`__