Skip to content

Commit 417f97f

Browse files
committed
Update reference docs for legacy MongoClient API to use MongoClientSettings and ConnectionString
JAVA-3659
1 parent 941dffe commit 417f97f

File tree

8 files changed

+112
-133
lines changed

8 files changed

+112
-133
lines changed

docs/reference/content/driver/getting-started/quick-start-pojo.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ that can be found with the driver source on github.
3030
```java
3131
import com.mongodb.Block;
3232
import com.mongodb.MongoClient;
33-
import com.mongodb.MongoClientURI;
33+
import com.mongodb.MongoClientSettings;
3434
import com.mongodb.client.MongoCollection;
3535
import com.mongodb.client.MongoDatabase;
3636
import com.mongodb.client.result.DeleteResult;

docs/reference/content/driver/getting-started/quick-start.md

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Legacy MongoClient API:
3535

3636
```java
3737
import com.mongodb.MongoClient;
38-
import com.mongodb.MongoClientURI;
38+
import com.mongodb.ConnectionString;
3939
```
4040

4141
And the common elements between the legacy and new APIs:
@@ -130,21 +130,13 @@ MongoClient mongoClient = new MongoClient();
130130
- You can explicitly specify the hostname to connect to a MongoDB instance running on the specified host on port ``27017``:
131131

132132
```java
133-
MongoClient mongoClient = new MongoClient( "hostOne" );
134-
```
135-
136-
- You can explicitly specify the hostname and the port:
137-
138-
```java
139-
MongoClient mongoClient = new MongoClient( "hostOne" , 27018 );
133+
MongoClient mongoClient = new MongoClient("mongodb://hostOne");
140134
```
141135

142-
- You can specify the
143-
[`MongoClientURI`]({{< apiref "mongodb-driver-core" "com/mongodb/MongoClientURI.html" >}}) connection string:
136+
- You can explicitly specify the hostname and port:
144137

145138
```java
146-
MongoClientURI connectionString = new MongoClientURI("mongodb://hostOne:27017,hostTwo:27017");
147-
MongoClient mongoClient = new MongoClient(connectionString);
139+
MongoClient mongoClient = new MongoClient("mongodb://hostOne:27018");
148140
```
149141

150142
The connection string mostly follows [RFC 3986](http://tools.ietf.org/html/rfc3986), with the exception of the domain name. For MongoDB,

docs/reference/content/driver/reference/monitoring.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ The driver implements the
4949
[command monitoring specification](https://github.com/mongodb/specifications/blob/master/source/command-monitoring/command-monitoring.rst),
5050
allowing an application to be notified when a command starts and when it either succeeds or fails.
5151

52-
An application registers command listeners with a `MongoClient` by configuring `MongoClientOptions` with instances of classes
52+
An application registers command listeners with a `MongoClient` by configuring `MongoClientSettings` with instances of classes
5353
that implement the [`CommandListener`]({{< apiref "mongodb-driver-core" "com/mongodb/event/CommandListener" >}}) interface. Consider the following, somewhat
5454
simplistic, implementation of the `CommandListener` interface:
5555

@@ -113,7 +113,7 @@ The driver implements the
113113
[SDAM Monitoring specification](https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring-monitoring.rst),
114114
allowing an application to be notified when the driver detects changes to the topology of the MongoDB cluster to which it is connected.
115115

116-
An application registers listeners with a `MongoClient` by configuring `MongoClientOptions` with instances of classes that
116+
An application registers listeners with a `MongoClient` by configuring `MongoClientSettings` with instances of classes that
117117
implement any of the [`ClusterListener`]({{< apiref "mongodb-driver-core" "com/mongodb/event/ClusterListener" >}}),
118118
[`ServerListener`]({{< apiref "mongodb-driver-core" "com/mongodb/event/ServerListener" >}}),
119119
or [`ServerMonitorListener`]({{< apiref "mongodb-driver-core" "com/mongodb/event/ServerMonitorListener" >}}) interfaces.
@@ -194,7 +194,7 @@ and when that MongoClient is closed. In addition, it will print a message when
194194

195195
The driver supports monitoring of connection pool-related events.
196196

197-
An application registers listeners with a `MongoClient` by configuring `MongoClientOptions` with instances of classes that
197+
An application registers listeners with a `MongoClient` by configuring `MongoClientSettings` with instances of classes that
198198
implement the [`ConnectionPoolListener`]({{< apiref "mongodb-driver-core" "com/mongodb/event/ConnectionPoolListener" >}}) interface.
199199

200200
Consider the following, simplistic, example of a connection pool listener:

docs/reference/content/driver/tutorials/authentication.md

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,16 @@ Legacy MongoClient API:
3131

3232
```java
3333
import com.mongodb.MongoClient;
34-
import com.mongodb.MongoClientURI;
34+
import com.mongodb.ConnectionString;
3535
```
3636

3737
An authentication credential is represented as an instance of the
3838
[`MongoCredential`]({{< apiref "mongodb-driver-core" "com/mongodb/MongoCredential.html" >}}) class. The [`MongoCredential`]({{< apiref "mongodb-driver-core" "com/mongodb/MongoCredential.html" >}}) class includes static
3939
factory methods for each of the supported authentication mechanisms.
4040

41-
You can also use a [`MongoClientURI`]({{< apiref "mongodb-driver-core" "com/mongodb/MongoClientURI.html" >}}) and pass it to a
42-
[`MongoClient()`]({{< apiref "mongodb-driver-sync" "com/mongodb/client/MongoClient.html" >}}) constructor that takes a `MongoClientURI` parameter.
41+
You can also use a [`ConnectionString`]({{< apiref "mongodb-driver-core" "com/mongodb/ConnectionString.html" >}}) and pass it to a
42+
[`MongoClient()`]({{< apiref "mongodb-driver-legacy" "com/mongodb/MongoClient.html" >}}) constructor that takes a `ConnectionString`
43+
parameter.
4344

4445
## Default Authentication Mechanism
4546

@@ -76,7 +77,12 @@ MongoClient mongoClient = MongoClients.create(
7677
or using the legacy MongoClient API:
7778

7879
```java
79-
MongoClient mongoClient = new MongoClient(new ServerAddress("host1", 27017), credential);
80+
MongoClient mongoClient = new MongoClient(
81+
MongoClientSettings.builder()
82+
.applyToClusterSettings(builder ->
83+
builder.hosts(Arrays.asList(new ServerAddress("host1", 27017))))
84+
.credential(credential)
85+
.build());
8086
```
8187

8288
Or use a connection string without explicitly specifying the authentication mechanism. Using the new MongoClient API:
@@ -88,8 +94,7 @@ MongoClient mongoClient = MongoClients.create("mongodb://user1:pwd1@host1/?authS
8894
or using the legacy MongoClient API:
8995

9096
```java
91-
MongoClientURI uri = new MongoClientURI("mongodb://user1:pwd1@host1/?authSource=db1");
92-
MongoClient mongoClient = new MongoClient(uri);
97+
MongoClient mongoClient = new MongoClient("mongodb://user1:pwd1@host1/?authSource=db1");
9398
```
9499

95100
For challenge and response mechanisms, using the default authentication mechanism is the recommended approach as it will make upgrading
@@ -136,7 +141,12 @@ MongoClient mongoClient = MongoClients.create(
136141
or using the legacy MongoClient API:
137142

138143
```java
139-
MongoClient mongoClient = new MongoClient(new ServerAddress("host1", 27017), credential);
144+
MongoClient mongoClient = new MongoClient(
145+
MongoClientSettings.builder()
146+
.applyToClusterSettings(builder ->
147+
builder.hosts(Arrays.asList(new ServerAddress("host1", 27017))))
148+
.credential(credential)
149+
.build());
140150
```
141151

142152
Or use a connection string that explicitly specifies the `authMechanism=SCRAM-SHA-256`. Using the new MongoClient API:
@@ -148,8 +158,7 @@ MongoClient mongoClient = MongoClients.create("mongodb://user1:pwd1@host1/?authS
148158
or using the legacy MongoClient API:
149159

150160
```java
151-
MongoClientURI uri = new MongoClientURI("mongodb://user1:pwd1@host1/?authSource=db1&authMechanism=SCRAM-SHA-256");
152-
MongoClient mongoClient = new MongoClient(uri);
161+
MongoClient mongoClient = new MongoClient("mongodb://user1:pwd1@host1/?authSource=db1&authMechanism=SCRAM-SHA-256");
153162
```
154163

155164

@@ -181,7 +190,12 @@ MongoClient mongoClient = MongoClients.create(
181190
or using the legacy MongoClient API:
182191

183192
```java
184-
MongoClient mongoClient = new MongoClient(new ServerAddress("host1", 27017), credential);
193+
MongoClient mongoClient = new MongoClient(
194+
MongoClientSettings.builder()
195+
.applyToClusterSettings(builder ->
196+
builder.hosts(Arrays.asList(new ServerAddress("host1", 27017))))
197+
.credential(credential)
198+
.build());
185199
```
186200

187201
Or use a connection string that explicitly specifies the `authMechanism=SCRAM-SHA-1`. Using the new MongoClient API:
@@ -193,8 +207,7 @@ MongoClient mongoClient = MongoClients.create("mongodb://user1:pwd1@host1/?authS
193207
or using the legacy MongoClient API:
194208

195209
```java
196-
MongoClientURI uri = new MongoClientURI("mongodb://user1:pwd1@host1/?authSource=db1&authMechanism=SCRAM-SHA-1");
197-
MongoClient mongoClient = new MongoClient(uri);
210+
MongoClient mongoClient = new MongoClient("mongodb://user1:pwd1@host1/?authSource=db1&authMechanism=SCRAM-SHA-1");
198211
```
199212

200213
## MONGODB-CR
@@ -232,7 +245,12 @@ MongoClient mongoClient = MongoClients.create(
232245
or using the legacy MongoClient API:
233246

234247
```java
235-
MongoClient mongoClient = new MongoClient(new ServerAddress("host1", 27017), credential);
248+
MongoClient mongoClient = new MongoClient(
249+
MongoClientSettings.builder()
250+
.applyToClusterSettings(builder ->
251+
builder.hosts(Arrays.asList(new ServerAddress("host1", 27017))))
252+
.credential(credential)
253+
.build());
236254
```
237255

238256
Or use a connection string that explicitly specifies the `authMechanism=MONGODB-CR`. Using the new MongoClient API:
@@ -244,8 +262,7 @@ MongoClient mongoClient = MongoClients.create("mongodb://user1:pwd1@host1/?authS
244262
or using the legacy MongoClient API:
245263

246264
```java
247-
MongoClientURI uri = new MongoClientURI("mongodb://user1:pwd1@host1/?authSource=db1&authMechanism=MONGODB-CR");
248-
MongoClient mongoClient = new MongoClient(uri);
265+
MongoClient mongoClient = new MongoClient("mongodb://user1:pwd1@host1/?authSource=db1&authMechanism=MONGODB-CR");
249266
```
250267

251268
{{% note %}}
@@ -285,8 +302,12 @@ MongoClient mongoClient = MongoClients.create(
285302
or using the legacy MongoClient API:
286303

287304
```java
288-
MongoClientOptions options = MongoClientOptions.builder().sslEnabled(true).build();
289-
MongoClient mongoClient = new MongoClient(new ServerAddress("host1", 27017), credential, options);
305+
MongoClient mongoClient = new MongoClient(
306+
MongoClientSettings.builder()
307+
.applyToClusterSettings(builder ->
308+
builder.hosts(Arrays.asList(new ServerAddress("host1", 27017))))
309+
.credential(credential)
310+
.build());
290311
```
291312

292313
Or use a connection string that explicitly specifies the `authMechanism=MONGODB-X509`. Using the new MongoClient API:
@@ -298,8 +319,7 @@ MongoClient mongoClient = MongoClients.create("mongodb://subjectName@host1/?auth
298319
or using the legacy MongoClient API:
299320

300321
```java
301-
MongoClientURI uri = new MongoClientURI("mongodb://subjectName@host1/?authMechanism=MONGODB-X509&ssl=true");
302-
MongoClient mongoClient = new MongoClient(uri);
322+
MongoClient mongoClient = new MongoClient("mongodb://subjectName@host1/?authMechanism=MONGODB-X509&ssl=true");
303323
```
304324

305325
See the MongoDB server [x.509 tutorial]({{<docsref "tutorial/configure-x509-client-authentication/#add-x-509-certificate-subject-as-a-user" >}})
@@ -434,7 +454,12 @@ MongoClient mongoClient = MongoClients.create(
434454
or using the legacy MongoClient API:
435455

436456
```java
437-
MongoClient mongoClient = new MongoClient(new ServerAddress("host1", 27017), credential);
457+
MongoClient mongoClient = new MongoClient(
458+
MongoClientSettings.builder()
459+
.applyToClusterSettings(builder ->
460+
builder.hosts(Arrays.asList(new ServerAddress("host1", 27017))))
461+
.credential(credential)
462+
.build());
438463
```
439464

440465
Or use a connection string that explicitly specifies the `authMechanism=GSSAPI`. Using the new MongoClient API:
@@ -446,7 +471,7 @@ MongoClient mongoClient = MongoClients.create("mongodb://username%40REALM.ME@hos
446471
or using the legacy MongoClient API:
447472

448473
```java
449-
MongoClientURI uri = new MongoClientURI("mongodb://username%40REALM.ME@host1/?authMechanism=GSSAPI");
474+
MongoClient mongoClient = new MongoClient("mongodb://username%40REALM.ME@host1/?authMechanism=GSSAPI");
450475
```
451476
{{% note %}}
452477

@@ -528,7 +553,12 @@ MongoClient mongoClient = MongoClients.create(
528553
or using the legacy MongoClient API:
529554

530555
```java
531-
MongoClient mongoClient = new MongoClient(new ServerAddress("host1", 27017), credential);
556+
MongoClient mongoClient = new MongoClient(
557+
MongoClientSettings.builder()
558+
.applyToClusterSettings(builder ->
559+
builder.hosts(Arrays.asList(new ServerAddress("host1", 27017))))
560+
.credential(credential)
561+
.build());
532562
```
533563

534564
Or use a connection string that explicitly specifies the `authMechanism=PLAIN`. Using the new MongoClient API:
@@ -540,7 +570,7 @@ MongoClient mongoClient = MongoClients.create("mongodb://user1@host1/?authSource
540570
or using the legacy MongoClient API:
541571

542572
```java
543-
MongoClientURI uri = new MongoClientURI("mongodb://user1@host1/?authSource=$external&authMechanism=PLAIN");
573+
MongoClient mongoClient = new MongoClient("mongodb://user1@host1/?authSource=$external&authMechanism=PLAIN");
544574
```
545575

546576
{{% note %}}

docs/reference/content/driver/tutorials/compression.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ release.
2121
The driver will negotiate which, if any, compression algorithm is used based on capabilities advertised by the server in
2222
the [ismaster]({{<docsref "reference/command/isMaster/" >}}) command response.
2323

24-
### Specify compression via `MongoClientURI`
24+
### Specify compression via `ConnectionString`
2525

2626
```java
2727
import com.mongodb.ConnectionString;

0 commit comments

Comments
 (0)