@@ -84,13 +84,109 @@ when running multiple concurrent operations.
84
84
- To dispose of an instance, call the ``MongoClient.close()`` method
85
85
to clean up resources.
86
86
87
- Connect to a Standalone MongoDB Deployment
88
- ------------------------------------------
87
+ Connection URI
88
+ --------------
89
89
90
- The following example shows several ways to connect to a single
91
- MongoDB deployment.
90
+ The **connection URI** provides a set of instructions that the driver uses to
91
+ connect to a MongoDB deployment. It instructs the driver on how it should
92
+ connect to MongoDB and how it should behave while connected. The following
93
+ figure explains each part of a sample connection URI:
92
94
93
- You can connect to a single MongoDB deployment in the following ways:
95
+ .. figure:: /includes/dns_seedlist_connection_string_parts.png
96
+ :alt: An example of a connection string that demonstrates the protocol, credentials, hostname or IP, port, and connection options
97
+
98
+ In this example, you connect to an Atlas MongoDB deployment that has a
99
+ DNS SRV record. For more details, see the :manual:`DNS Seed List
100
+ Connection Format
101
+ </reference/connection-string/#dns-seed-list-connection-format>`
102
+ documentation. This format offers flexibility in deployment and the
103
+ ability to change the servers in rotation without reconfiguring clients.
104
+
105
+ .. note::
106
+
107
+ If your deployment is on MongoDB Atlas, see the
108
+ :atlas:`Atlas driver connection guide </driver-connection/>`
109
+ and select :guilabel:`Scala` from the language dropdown to retrieve your connection
110
+ string.
111
+
112
+ If you are connecting to an instance or replica set that does not have a
113
+ DNS SRV address, you must use ``mongodb`` for the protocol, which specifies
114
+ the :manual:`Standard Connection String Format
115
+ </reference/connection-string/#std-label-connections-standard-connection-string-format>`.
116
+
117
+ After the protocol, the connection string contains your
118
+ credentials if you are using a password-based authentication mechanism.
119
+ Replace the value of ``user`` with your username and ``pass`` with your
120
+ password. If your authentication mechanism does not require credentials,
121
+ omit this part of the connection URI.
122
+
123
+ The next part of the connection URI specifies the hostname or IP
124
+ address, followed by the port of your MongoDB instance. In the example,
125
+ ``sample.host`` represents the hostname and ``27017`` is the port number.
126
+ Replace these values to refer to your MongoDB instance.
127
+
128
+ The last part of the connection URI contains connection options as parameters.
129
+ The example sets two connection options: ``maxPoolSize=20`` and
130
+ ``w=majority``.
131
+
132
+ Connect to MongoDB Atlas
133
+ ------------------------
134
+
135
+ You can use the following connection snippet to test your connection to
136
+ your MongoDB deployment on Atlas:
137
+
138
+ .. code-block:: scala
139
+ :emphasize-lines: 14
140
+
141
+ import com.mongodb.{ServerApi, ServerApiVersion}
142
+ import org.mongodb.scala.{ConnectionString, MongoClient, MongoClientSettings}
143
+ import org.mongodb.scala.bson.Document
144
+
145
+ import scala.concurrent.Await
146
+ import scala.concurrent.duration.DurationInt
147
+ import scala.util.Using
148
+
149
+ object MongoClientConnectionExample {
150
+
151
+ def main(args: Array[String]): Unit = {
152
+
153
+ // Replace the placeholder with your Atlas connection string
154
+ val connectionString = "<connection string>";
155
+
156
+ // Construct a ServerApi instance using the ServerApi.builder() method
157
+ val serverApi = ServerApi.builder.version(ServerApiVersion.V1).build()
158
+
159
+ val settings = MongoClientSettings
160
+ .builder()
161
+ .applyConnectionString(ConnectionString(connectionString))
162
+ .serverApi(serverApi)
163
+ .build()
164
+
165
+ // Create a new client and connect to the server
166
+ Using(MongoClient(settings)) { mongoClient =>
167
+ // Send a ping to confirm a successful connection
168
+ val database = mongoClient.getDatabase("admin")
169
+ val ping = database.runCommand(Document("ping" -> 1)).head()
170
+
171
+ Await.result(ping, 10.seconds)
172
+ System.out.println("Pinged your deployment. You successfully connected to MongoDB!")
173
+ }
174
+ }
175
+ }
176
+
177
+ This connection snippet uses the Stable API feature, which you can
178
+ enable when using the {+driver-short+} v4.3 and later to connect to MongoDB
179
+ Server v5.0 and later. When you use this feature, you can update your
180
+ driver or server without worrying about backward compatibility issues
181
+ with any commands covered by the Stable API.
182
+
183
+ To learn more about the Stable API feature, see
184
+ :manual:`Stable API </reference/stable-api/>` in the Server manual.
185
+
186
+ Connect to a Local MongoDB Deployment
187
+ -------------------------------------
188
+
189
+ You can connect to a local MongoDB deployment in the following ways:
94
190
95
191
- Instantiate a ``MongoClient`` object without any parameters to
96
192
connect to a MongoDB server running on localhost on port ``27017``:
0 commit comments