Skip to content

Commit 5aba92d

Browse files
authored
Respond to CC feedback on #238 (#239)
* expand code intro * fixes * link and wording fixes * collaborated changes * edits
1 parent 3610c45 commit 5aba92d

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

source/fundamentals/connection.txt

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ deployment using the Go Driver.
1919
Connection URI
2020
--------------
2121

22-
The **connection URI** provides a set of instructions that the driver uses to
23-
connect to a MongoDB deployment. It instructs the driver on how it should
24-
connect to MongoDB and how it should behave while connected. The following
25-
example explains each part of a sample connection URI:
22+
A **connection URI**, also known as a connection string, tells the
23+
driver how to connect to MongoDB and how to behave while connected.
24+
25+
Parts of a Connection URI
26+
~~~~~~~~~~~~~~~~~~~~~~~~~
27+
28+
The following example explains each part of a sample connection URI:
2629

2730
.. figure:: /includes/figures/connection_uri_parts.png
2831
:alt: Each part of the connection string
@@ -55,10 +58,31 @@ options. In the example, we set two connection options:
5558
``maxPoolSize=20`` and ``w=majority``. To learn more about connection
5659
options, read the :ref:`golang-connection-options` section of this guide.
5760

58-
The following code shows how you can use a connection string in a
59-
client to connect to MongoDB. In this example, the client uses
60-
the :ref:`Stable API <golang-stable-api>` feature, though
61-
you can connect to MongoDB without this feature.
61+
Connection Example
62+
~~~~~~~~~~~~~~~~~~
63+
64+
To connect to MongoDB, you need to create a client. A client manages
65+
your connections and runs database commands.
66+
67+
You can create a client that uses your connection string and other
68+
client options by passing a ``ClientOptions`` object to the ``Connect()``
69+
method. To specify your connection URI, pass it to the ``ApplyURI()``
70+
method, creating a ``ClientOptions`` instance. To set any other
71+
options, call the relevant helper method from the ``options`` package.
72+
73+
To learn more about connection options, see the
74+
:ref:`Connection Options section <golang-connection-options>`. To learn
75+
more about creating a client, see the API documentation for `Client
76+
<{+api+}/mongo#Client>`__ and `Connect() <{+api+}/mongo#Connect>`__.
77+
78+
You can set the {+stable-api+} version as an option to avoid
79+
breaking changes when you upgrade to a new server version. To
80+
learn more about the {+stable-api+} feature, see the :ref:`{+stable-api+} page
81+
<golang-stable-api>`.
82+
83+
The following code shows how you can create a client that uses your
84+
connection string and the {+stable-api+} version, connect to MongoDB, and
85+
verify that the connection is successful:
6286

6387
.. literalinclude:: /includes/fundamentals/code-snippets/srv.go
6488
:language: go

source/includes/fundamentals/code-snippets/srv.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const uri = "<connection string>"
1414

1515
func main() {
1616

17+
// Use the SetServerAPIOptions() method to set the Stable API version to 1
1718
serverAPI := options.ServerAPI(options.ServerAPIVersion1)
1819
opts := options.Client().ApplyURI(uri).SetServerAPIOptions(serverAPI)
1920

@@ -29,10 +30,10 @@ func main() {
2930
}
3031
}()
3132

32-
// Ping the primary
33+
// Send a ping to confirm a successful connection
3334
if err := client.Ping(context.TODO(), readpref.Primary()); err != nil {
3435
panic(err)
3536
}
3637

37-
fmt.Println("Successfully connected and pinged.")
38+
fmt.Println("Pinged the primary node of the cluster. You successfully connected to MongoDB!")
3839
}

0 commit comments

Comments
 (0)