Skip to content

Commit 1a0133c

Browse files
authored
changes following from C# (#240)
(cherry picked from commit a389d042923eaca77641672eae89840f8f1b504a)
1 parent 5aba92d commit 1a0133c

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

source/fundamentals/connection.txt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@ You can also use the :manual:`DNS Seed List Connection Format
3838
want more flexibility of deployment and the ability to change the
3939
servers in rotation without reconfiguring clients.
4040

41-
.. note::
42-
43-
To learn how to retrieve your connection string in Atlas, see the :ref:`Quick Start guide <golang-connect-to-your-cluster>`.
44-
4541
The next part of the connection string contains your username and, if
4642
you are using password-based authentication, your password. Replace the value of
4743
``user`` with your username and ``pass`` with your password. If you are using an
@@ -66,8 +62,10 @@ your connections and runs database commands.
6662

6763
You can create a client that uses your connection string and other
6864
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
65+
method.
66+
67+
To specify your connection URI, pass it to the ``ApplyURI()``
68+
method, which returns a new ``ClientOptions`` instance. To set any other
7169
options, call the relevant helper method from the ``options`` package.
7270

7371
To learn more about connection options, see the
@@ -80,13 +78,18 @@ breaking changes when you upgrade to a new server version. To
8078
learn more about the {+stable-api+} feature, see the :ref:`{+stable-api+} page
8179
<golang-stable-api>`.
8280

83-
The following code shows how you can create a client that uses your
81+
The following code shows how you can create a client that uses an Atlas
8482
connection string and the {+stable-api+} version, connect to MongoDB, and
8583
verify that the connection is successful:
8684

8785
.. literalinclude:: /includes/fundamentals/code-snippets/srv.go
8886
:language: go
8987

88+
.. tip::
89+
90+
Follow the :ref:`Quick Start guide <golang-connect-to-your-cluster>`
91+
to retrieve your Atlas connection string.
92+
9093
.. note::
9194

9295
To learn about connecting to Atlas Serverless, see the

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import (
44
"context"
55
"fmt"
66

7+
"go.mongodb.org/mongo-driver/bson"
78
"go.mongodb.org/mongo-driver/mongo"
89
"go.mongodb.org/mongo-driver/mongo/options"
9-
"go.mongodb.org/mongo-driver/mongo/readpref"
1010
)
1111

12-
// Paste your connection string URI here
13-
const uri = "<connection string>"
12+
// Replace the placeholders with your credentials
13+
const uri = "mongodb+srv://<username>:<password>@cluster0.sample.mongodb.net/?retryWrites=true&w=majority"
1414

1515
func main() {
1616

@@ -31,9 +31,9 @@ func main() {
3131
}()
3232

3333
// Send a ping to confirm a successful connection
34-
if err := client.Ping(context.TODO(), readpref.Primary()); err != nil {
34+
var result bson.M
35+
if err := client.Database("admin").RunCommand(context.TODO(), bson.D{{"ping", 1}}).Decode(&result); err != nil {
3536
panic(err)
3637
}
37-
38-
fmt.Println("Pinged the primary node of the cluster. You successfully connected to MongoDB!")
38+
fmt.Println("Pinged your deployment. You successfully connected to MongoDB!")
3939
}

0 commit comments

Comments
 (0)