Skip to content

Commit 4752cd1

Browse files
committed
JAVA-2751: Reference documentation for new MongoClients entry point to the synchronous driver
1 parent e95aca0 commit 4752cd1

18 files changed

+595
-128
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ There are multiple ways to set the `pojoCodecRegistry` for use:
149149
- You can set it when instantiating a MongoClient object:
150150

151151
```java
152-
MongoClient mongoClient = new MongoClient("localhost", MongoClientOptions.builder().codecRegistry(pojoCodecRegistry).build());
152+
MongoClientSettings settings = MongoClientSettings.builder()
153+
.codecRegistry(pojoCodecRegistry)
154+
.build();
155+
MongoClient mongoClient = MongoClients.create(settings);
153156
```
154157

155158
- You can use an alternative `CodecRegistry` with a `MongoDatabase`:

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

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,24 @@ that can be found with the driver source on github.
2323

2424
- The following import statements:
2525

26+
New MongoClient API (since 3.7):
27+
28+
```java
29+
import com.mongodb.ConnectionString;
30+
import com.mongodb.clients.MongoClients;
31+
import com.mongodb.clients.MongoClient;
32+
```
33+
34+
Legacy MongoClient API:
35+
2636
```java
2737
import com.mongodb.MongoClient;
2838
import com.mongodb.MongoClientURI;
39+
```
40+
41+
And the common elements between the legacy and new APIs:
42+
43+
```java
2944
import com.mongodb.ServerAddress;
3045

3146
import com.mongodb.client.MongoDatabase;
@@ -45,7 +60,9 @@ import java.util.List;
4560
```
4661
## Make a Connection
4762

48-
Use [`MongoClient()`]({{< apiref "com/mongodb/MongoClient.html">}}) to make a connection to a running MongoDB instance.
63+
Use [`MongoClients.create()`]({{< apiref "com/mongodb/client/MongoClients.html">}}),
64+
or [`MongoClient()`]({{< apiref "com/mongodb/MongoClient.html">}}) for the legacy MongoClient API,
65+
to make a connection to a running MongoDB instance.
4966

5067
The `MongoClient` instance represents a pool of connections to the database; you will only need one instance of class `MongoClient` even with multiple threads.
5168

@@ -61,9 +78,46 @@ The `MongoClient` instance represents a pool of connections to the database; you
6178

6279
### Connect to a Single MongoDB instance
6380

64-
The following example shows five ways to connect to the
65-
database `mydb` on the local machine. If the database does not exist, MongoDB
66-
will create it for you.
81+
The following example shows several ways to connect to a single MongoDB server.
82+
83+
##### New MongoClient API (since 3.7)
84+
85+
To connect to a single MongoDB instance:
86+
87+
- You can instantiate a MongoClient object without any parameters to connect to a MongoDB instance running on localhost on port ``27017``:
88+
89+
```java
90+
MongoClient mongoClient = MongoClients.create();
91+
```
92+
93+
- You can explicitly specify the hostname to connect to a MongoDB instance running on the specified host on port ``27017``:
94+
95+
```java
96+
MongoClient mongoClient = MongoClients.create(
97+
MongoClientSettings.builder()
98+
.applyToClusterSettings(builder ->
99+
builder.hosts(Arrays.asList(new ServerAddress("hostOne"))))
100+
.build());
101+
```
102+
103+
- You can explicitly specify the hostname and the port:
104+
105+
```java
106+
MongoClient mongoClient = MongoClients.create(
107+
MongoClientSettings.builder()
108+
.applyToClusterSettings(builder ->
109+
builder.hosts(Arrays.asList(new ServerAddress("hostOne", 27018))))
110+
.build());
111+
```
112+
113+
- You can specify the [`ConnectionString`]({{< apiref "/com/mongodb/ConnectionString.html">}}):
114+
115+
```java
116+
MongoClient mongoClient = MongoClients.create("mongodb://hostOne:27017,hostTwo:27018");
117+
```
118+
119+
120+
##### Legacy MongoClient API
67121

68122
To connect to a single MongoDB instance:
69123

@@ -76,24 +130,26 @@ MongoClient mongoClient = new MongoClient();
76130
- You can explicitly specify the hostname to connect to a MongoDB instance running on the specified host on port ``27017``:
77131

78132
```java
79-
MongoClient mongoClient = new MongoClient( "localhost" );
133+
MongoClient mongoClient = new MongoClient( "hostOne" );
80134
```
81135

82136
- You can explicitly specify the hostname and the port:
83137

84138
```java
85-
MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
139+
MongoClient mongoClient = new MongoClient( "hostOne" , 27018 );
86140
```
87141

88142
- You can specify the
89143
[`MongoClientURI`]({{< apiref "/com/mongodb/MongoClientURI.html">}}) connection string:
90144

91145
```java
92-
MongoClientURI connectionString = new MongoClientURI("mongodb://localhost:27017");
146+
MongoClientURI connectionString = new MongoClientURI("mongodb://hostOne:27017,hostTwo:27017");
93147
MongoClient mongoClient = new MongoClient(connectionString);
94148
```
95149

96-
The connection string mostly follows [RFC 3986](http://tools.ietf.org/html/rfc3986), with the exception of the domain name. For MongoDB, it is possible to list multiple domain names separated by a comma. For more information on the connection string, see [connection string]({{< docsref "reference/connection-string" >}}).
150+
The connection string mostly follows [RFC 3986](http://tools.ietf.org/html/rfc3986), with the exception of the domain name. For MongoDB,
151+
it is possible to list multiple domain names separated by a comma. For more information on the connection string, see
152+
[connection string]({{< docsref "reference/connection-string" >}}).
97153

98154
## Access a Database
99155

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,11 @@ public class TestCommandListener implements CommandListener {
100100
and an instance of `MongoClientOptions` configured with an instance of `TestCommandListener`:
101101

102102
```java
103-
MongoClientOptions options = MongoClientOptions.builder()
104-
.addCommandListener(new TestCommandListener())
105-
.build();
106-
MongoClient client = new MongoClient(new ServerAddress(), options);
103+
MongoClientSettings settings = MongoClientSettings.builder()
104+
.addCommandListener(new TestCommandListener())
105+
.build();
106+
MongoClient client = MongoClients.create(settings);
107+
107108
```
108109

109110
A `MongoClient` configured with these options will print a message to `System.out` before sending each command to a MongoDB server, and
@@ -177,10 +178,11 @@ and an instance of `MongoClientOptions` configured with an instance of `TestClus
177178

178179
```java
179180
List<ServerAddress> seedList = ...
180-
MongoClientOptions options = MongoClientOptions.builder()
181-
.addClusterListener(new TestClusterListener(ReadPreference.secondary()))
182-
.build();
183-
MongoClient client = new MongoClient(seedList, options);
181+
MongoClientSettings settings = MongoClientSettings.builder()
182+
.applyToClusterSettings(builder ->
183+
builder.addClusterListener(new TestClusterListener(ReadPreference.secondary())))
184+
.build();
185+
MongoClient client = MongoClients.create(settings);
184186
```
185187

186188
A `MongoClient` configured with these options will print a message to `System.out` when the MongoClient is created with these options,
@@ -248,10 +250,11 @@ and an instance of `MongoClientOptions` configured with an instance of `TestConn
248250

249251
```java
250252
List<ServerAddress> seedList = ...
251-
MongoClientOptions options = MongoClientOptions.builder()
252-
.addConnectionPoolListener(new TestConnectionPoolListener())
253-
.build();
254-
MongoClient client = new MongoClient(new ServerAddress(), options);
253+
MongoClientSettings settings = MongoClientSettings.builder()
254+
.applyToConnectionPoolSettings(builder ->
255+
builder.addConnectionPoolListener(new TestConnectionPoolListener()))
256+
.build();
257+
MongoClient client = MongoClients.create(settings);
255258
```
256259

257260
A `MongoClient` configured with these options will print a message to `System.out` for each connection pool-related event for each MongoDB

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ The [aggregation pipeline]({{<docsref "core/aggregation-pipeline">}}) is a frame
2020

2121
```java
2222
import com.mongodb.Block;
23-
import com.mongodb.MongoClient;
23+
import com.mongodb.client.MongoClients;
24+
import com.mongodb.client.MongoClient;
2425
import com.mongodb.client.MongoCollection;
2526
import com.mongodb.client.MongoDatabase;
2627
import com.mongodb.client.model.Aggregates;
@@ -49,7 +50,7 @@ Connect to a MongoDB deployment and declare and define a `MongoDatabase` and a `
4950
For example, include the following code to connect to a standalone MongoDB deployment running on localhost on port `27017` and define `database` to refer to the `test` database and `collection` to refer to the `restaurants` collection.
5051

5152
```java
52-
MongoClient mongoClient = new MongoClient();
53+
MongoClient mongoClient = MongoClients.create();
5354
MongoDatabase database = mongoClient.getDatabase("test");
5455
MongoCollection<Document> collection = database.getCollection("restaurants");
5556
```

0 commit comments

Comments
 (0)