Skip to content

Commit 3610c45

Browse files
authored
DOCSP-27920: atlas connection snippet w stableapi (#238)
* DOCSP-27920: atlas connection snippet w stableapi * MW suggestions
1 parent e914851 commit 3610c45

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

source/fundamentals/connection.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ options. In the example, we set two connection options:
5555
``maxPoolSize=20`` and ``w=majority``. To learn more about connection
5656
options, read the :ref:`golang-connection-options` section of this guide.
5757

58-
The following code shows how you can use the sample connection URI in a
59-
client to connect to MongoDB.
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.
6062

6163
.. literalinclude:: /includes/fundamentals/code-snippets/srv.go
6264
:language: go

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ import (
99
"go.mongodb.org/mongo-driver/mongo/readpref"
1010
)
1111

12-
// Connection URI
13-
const uri = "mongodb://user:[email protected]:27017/?maxPoolSize=20&w=majority"
12+
// Paste your connection string URI here
13+
const uri = "<connection string>"
1414

1515
func main() {
16+
17+
serverAPI := options.ServerAPI(options.ServerAPIVersion1)
18+
opts := options.Client().ApplyURI(uri).SetServerAPIOptions(serverAPI)
19+
1620
// Create a new client and connect to the server
17-
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
21+
client, err := mongo.Connect(context.TODO(), opts)
1822

1923
if err != nil {
2024
panic(err)

0 commit comments

Comments
 (0)