Skip to content

Commit 311e4f6

Browse files
authored
Merge branch 'standardization' into docsp-45207-aggregation
2 parents 54997a9 + e9ead1c commit 311e4f6

38 files changed

+3638
-19
lines changed

snooty.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ toc_landing_pages = [
44
"/get-started",
55
"/connect",
66
"/write",
7-
"/indexes"
7+
"/indexes",
8+
"/databases-collection",
9+
"/security/authentication"
810
]
911

1012
intersphinx = ["https://www.mongodb.com/docs/manual/objects.inv"]

source/connect.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@ Connect to MongoDB
2222
:titlesonly:
2323
:maxdepth: 1
2424

25-
/connect/mongoclient
26-
/connect/stable-api
25+
Create a Client </connect/mongoclient>
26+
Stable API </connect/stable-api>
27+
Choose a Connection Target </connect/connection-targets>
28+
Configure TLS </connect/tls>

source/connect/connection-targets.txt

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
.. _ruby-connection-targets:
2+
3+
==========================
4+
Choose a Connection Target
5+
==========================
6+
7+
.. facet::
8+
:name: genre
9+
:values: reference
10+
11+
.. meta::
12+
:keywords: connection string, URI, server, settings, client
13+
14+
.. contents:: On this page
15+
:local:
16+
:backlinks: none
17+
:depth: 2
18+
:class: singlecol
19+
20+
Overview
21+
--------
22+
23+
In this guide, you can learn how to use a connection string and a ``Mongo::Client`` object
24+
to connect to different types of MongoDB deployments.
25+
26+
Atlas
27+
-----
28+
29+
To connect to a MongoDB deployment on Atlas, include the following elements
30+
in your connection string:
31+
32+
- The URL of your Atlas cluster
33+
- Your MongoDB username
34+
- Your MongoDB password
35+
36+
Then, pass your connection string to the ``Mongo::Client`` constructor.
37+
38+
.. tip::
39+
40+
Follow the :atlas:`Atlas driver connection guide </driver-connection>`
41+
to retrieve your connection string.
42+
43+
When you connect to Atlas, we recommend using the {+stable-api+} client option to avoid
44+
breaking changes when Atlas upgrades to a new version of {+mdb-server+}.
45+
To learn more about the {+stable-api+} feature, see the :ref:`<ruby-stable-api>`
46+
guide.
47+
48+
The following code shows how to use the {+driver-short+} to connect to an Atlas cluster. The
49+
code also uses the ``server_api`` field to specify a {+stable-api+} version.
50+
51+
.. literalinclude:: /includes/connect/connection-targets.rb
52+
:language: ruby
53+
:start-after: start-connection-target-atlas
54+
:end-before: end-connection-target-atlas
55+
:dedent:
56+
57+
Local Deployments
58+
-----------------
59+
60+
To connect to a local standalone MongoDB deployment, specify the host of the
61+
server. Optionally, specify the port of the server and the database to connect
62+
to. If no port is specified, the default port is ``27017``. If no database name
63+
is specified, the client will use the ``admin`` database:
64+
65+
.. literalinclude:: /includes/connect/connection-targets.rb
66+
:language: ruby
67+
:start-after: start-local-connection
68+
:end-before: end-local-connection
69+
:dedent:
70+
71+
You can also specify the host, port, and database to connect to using a
72+
connection string:
73+
74+
.. literalinclude:: /includes/connect/connection-targets.rb
75+
:language: ruby
76+
:start-after: start-local-connection-uri
77+
:end-before: end-local-connection-uri
78+
:dedent:
79+
80+
You can also specify your host as ``localhost``. The following code example
81+
connects to ``localhost`` on the default port, ``27017``:
82+
83+
.. literalinclude:: /includes/connect/connection-targets.rb
84+
:language: ruby
85+
:start-after: start-localhost
86+
:end-before: end-localhost
87+
:dedent:
88+
89+
Replica Sets
90+
------------
91+
92+
To connect to a replica set, it is recommended to specify all nodes that are
93+
part of the replica set. In the event that one or more nodes becomes unavailable,
94+
specifying all nodes allows the driver to still connect to the replica set,
95+
as long as one node is available.
96+
97+
However, it is sufficient to pass the address of any one node in the replica set
98+
to the driver. The node does not have to be the primary, and it may be a hidden node.
99+
The driver will then automatically discover the remaining nodes.
100+
101+
The following example shows how to specify three members of the replica set:
102+
103+
.. literalinclude:: /includes/connect/connection-targets.rb
104+
:language: ruby
105+
:start-after: start-replica-set
106+
:end-before: end-replica-set
107+
:dedent:
108+
109+
The following example shows how to connect to the replica set using a connection
110+
string:
111+
112+
.. literalinclude:: /includes/connect/connection-targets.rb
113+
:language: ruby
114+
:start-after: start-replica-set-uri
115+
:end-before: end-replica-set-uri
116+
:dedent:
117+
118+
The following example shows how to verify the replica set name upon connection
119+
by using the ``replica_set`` option or the ``replicaSet`` connection string option:
120+
121+
.. literalinclude:: /includes/connect/connection-targets.rb
122+
:language: ruby
123+
:start-after: start-replica-set-option
124+
:end-before: end-replica-set-option
125+
:dedent:
126+
127+
API Documentation
128+
-----------------
129+
130+
To learn more about creating a ``Mongo::Client`` object with the {+driver-short+},
131+
see the API documentation for `Mongo::Client <{+api-root+}/Mongo/Client.html>`__ .

0 commit comments

Comments
 (0)