Skip to content

Commit 1fb6902

Browse files
authored
DOCSP-28358: scala stableapi connection updates (#862)
* DOCSP-28358: scala stable api connection updates * code updates * MW suggestions + copied to other driver pages
1 parent f70fa00 commit 1fb6902

File tree

5 files changed

+85
-31
lines changed

5 files changed

+85
-31
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import org.mongodb.scala.MongoClient
2+
import org.mongodb.scala.bson.Document
3+
4+
import scala.concurrent.Await
5+
import scala.concurrent.duration.DurationInt
6+
import scala.util.Using
7+
8+
object MongoClientConnectionExample {
9+
10+
def main(args: Array[String]): Unit = {
11+
12+
// Replace the placeholder with your Atlas connection string
13+
val connectionString = "<connection string>";
14+
15+
// Create a new client and connect to the server
16+
Using(MongoClient(connectionString)) { mongoClient =>
17+
// Send a ping to confirm a successful connection
18+
val database = mongoClient.getDatabase("admin")
19+
val ping = database.runCommand(Document("ping" -> 1)).head()
20+
21+
Await.result(ping, 10.seconds)
22+
System.out.println("Pinged your deployment. You successfully connected to MongoDB!")
23+
}
24+
}
25+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import com.mongodb.{ServerApi, ServerApiVersion}
2+
import org.mongodb.scala.{ConnectionString, MongoClient, MongoClientSettings}
3+
import org.mongodb.scala.bson.Document
4+
5+
import scala.concurrent.Await
6+
import scala.concurrent.duration.DurationInt
7+
import scala.util.Using
8+
9+
object MongoClientConnectionExample {
10+
11+
def main(args: Array[String]): Unit = {
12+
13+
// Replace the placeholder with your Atlas connection string
14+
val connectionString = "<connection string>";
15+
16+
// Construct a ServerApi instance using the ServerApi.builder() method
17+
val serverApi = ServerApi.builder.version(ServerApiVersion.V1).build()
18+
19+
val settings = MongoClientSettings
20+
.builder()
21+
.applyConnectionString(ConnectionString(connectionString))
22+
.serverApi(serverApi)
23+
.build()
24+
25+
// Create a new client and connect to the server
26+
Using(MongoClient(settings)) { mongoClient =>
27+
// Send a ping to confirm a successful connection
28+
val database = mongoClient.getDatabase("admin")
29+
val ping = database.runCommand(Document("ping" -> 1)).head()
30+
31+
Await.result(ping, 10.seconds)
32+
System.out.println("Pinged your deployment. You successfully connected to MongoDB!")
33+
}
34+
}
35+
}

source/php.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ v5.0 and later. When you use this feature, you can update your driver or server
118118
worrying about backward compatibility issues with any commands covered by the
119119
{+stable-api+}.
120120

121-
To learn more about the {+stable-api+} feature, see the :manual:`Server
122-
manual </reference/stable-api/>`.
121+
To learn more about the {+stable-api+} feature, see
122+
:manual:`{+stable-api+} </reference/stable-api/>` in the Server manual.
123123

124124
.. include:: /includes/stable-api-notice.rst
125125

@@ -129,7 +129,7 @@ Connect to MongoDB Atlas Without the Stable API
129129
-----------------------------------------------
130130

131131
If you are using a version of MongoDB or the driver that doesn't support the
132-
{+stable-api+}, you can use the following code snippet to test your connection
132+
{+stable-api+} feature, you can use the following code snippet to test your connection
133133
to your MongoDB deployment on Atlas:
134134

135135
.. literalinclude:: /includes/connection-snippets/scram/php-connection-no-stableapi.php

source/rust.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ Server v5.0 and later. When you use this feature, you can update your driver or
7676
worrying about backward compatibility issues with any commands covered by the
7777
{+stable-api+}.
7878

79-
To learn more about the {+stable-api+} feature, see the :manual:`Server
80-
manual </reference/stable-api/>`.
79+
To learn more about the {+stable-api+} feature, see
80+
:manual:`{+stable-api+} </reference/stable-api/>` in the Server manual.
8181

8282
.. include:: /includes/stable-api-notice.rst
8383

@@ -87,7 +87,7 @@ Connect to MongoDB Atlas Without the Stable API
8787
-----------------------------------------------
8888

8989
If you are using a version of MongoDB or the driver that doesn't support the
90-
{+stable-api+}, you can use the connection snippets in the following
90+
{+stable-api+} feature, you can use the connection snippets in the following
9191
tabs to test your connection to your MongoDB deployment on Atlas. Select from the
9292
:guilabel:`Sync` or :guilabel:`Async` tabs below for corresponding
9393
connection code samples.

source/scala.txt

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -46,39 +46,33 @@ for more information.
4646
Connect to MongoDB Atlas
4747
------------------------
4848

49-
.. include:: /includes/atlas-connect-blurb.rst
49+
You can use the following connection snippet to test your connection to
50+
your MongoDB deployment on Atlas:
5051

51-
.. code-block:: scala
52-
53-
import org.mongodb.scala._
54-
55-
// ...
56-
57-
// Replace the uri string with your MongoDB deployment's connection string.
58-
val uri: String = "mongodb+srv://<username>:<password>@<cluster-address>/test?retryWrites=true&w=majority"
59-
System.setProperty("org.mongodb.async.type", "netty")
60-
val client: MongoClient = MongoClient(uri)
61-
val db: MongoDatabase = client.getDatabase("test")
52+
.. literalinclude:: /includes/connection-snippets/scram/scala-connection.scala
53+
:language: scala
6254

63-
.. include:: /includes/serverless-compatibility.rst
55+
This connection snippet uses the {+stable-api+} feature, which you can
56+
enable when using the Scala driver v4.3 and later to connect to MongoDB Server
57+
v5.0 and later. When you use this feature, you can update your driver or server without
58+
worrying about backward compatibility issues with any commands covered by the
59+
{+stable-api+}.
6460

65-
See our guide on `Connecting <{+java-api+}/driver-scala/tutorials/connect-to-mongodb/>`__
66-
for more ways to connect.
61+
To learn more about the {+stable-api+} feature, see
62+
:manual:`{+stable-api+} </reference/stable-api/>` in the Server manual.
6763

68-
{+stable-api+}
69-
--------------
64+
.. include:: /includes/stable-api-notice.rst
7065

71-
You can use the {+stable-api+} feature starting with MongoDB Server version
72-
5.0 and Scala Driver version 4.3. When you use the {+stable-api+} feature,
73-
you can update your driver or server without worrying about backward
74-
compatibility issues with any commands covered by the {+stable-api+}.
66+
.. _connect-atlas-no-stable-api-scala-driver:
7567

76-
.. include:: /includes/stable-api-notice.rst
68+
Connect to MongoDB Atlas Without the Stable API
69+
-----------------------------------------------
7770

78-
To use this feature, construct a MongoDB client instance, specifying a version
79-
of the {+stable-api+}:
71+
If you are using a version of MongoDB or the driver that doesn't support the
72+
{+stable-api+} feature, you can use the following code snippet to test your connection
73+
to your MongoDB deployment on Atlas:
8074

81-
.. literalinclude:: /includes/stable-api-snippets/scala-client.scala
75+
.. literalinclude:: /includes/connection-snippets/scram/scala-connection-no-stableapi.scala
8276
:language: scala
8377

8478
Connect to a MongoDB Server on Your Local Machine

0 commit comments

Comments
 (0)