0
# Connection Management
1
2
Core client creation and connection management for MongoDB deployments including standalone servers, replica sets, and sharded clusters with automatic failover and connection pooling.
3
4
## Capabilities
5
6
### MongoClient (Legacy)
7
8
The primary entry point for connecting to MongoDB with built-in connection pooling and cluster management.
9
10
```java { .api }
11
/**
12
* MongoDB client with internal connection pooling for most applications
13
*/
14
public class MongoClient extends Mongo implements Closeable {
15
// Basic constructors
16
public MongoClient();
17
public MongoClient(String host);
18
public MongoClient(String host, int port);
19
public MongoClient(ServerAddress addr);
20
21
// Replica set / cluster constructors
22
public MongoClient(List<ServerAddress> seeds);
23
public MongoClient(List<ServerAddress> seeds, MongoClientOptions options);
24
25
// URI-based constructor
26
public MongoClient(MongoClientURI uri);
27
28
// Authentication constructors
29
public MongoClient(ServerAddress addr, MongoCredential credential, MongoClientOptions options);
30
public MongoClient(List<ServerAddress> seeds, MongoCredential credential, MongoClientOptions options);
31
32
// Database access
33
public MongoDatabase getDatabase(String databaseName);
34
35
// Database listing
36
public MongoIterable<String> listDatabaseNames();
37
public ListDatabasesIterable<Document> listDatabases();
38
39
// Session management
40
public ClientSession startSession();
41
public ClientSession startSession(ClientSessionOptions options);
42
43
// Change streams
44
public ChangeStreamIterable<Document> watch();
45
public ChangeStreamIterable<Document> watch(List<? extends Bson> pipeline);
46
47
// Resource management
48
public void close();
49
public MongoClientOptions getMongoClientOptions();
50
}
51
```
52
53
**Basic Connection Examples:**
54
55
```java
56
// Connect to localhost:27017
57
MongoClient client = new MongoClient();
58
59
// Connect to specific host and port
60
MongoClient client = new MongoClient("mongodb.example.com", 27017);
61
62
// Connect using server address
63
MongoClient client = new MongoClient(new ServerAddress("mongodb.example.com", 27017));
64
```
65
66
**Replica Set Connection:**
67
68
```java
69
// Connect to replica set
70
List<ServerAddress> seeds = Arrays.asList(
71
new ServerAddress("mongo1.example.com", 27017),
72
new ServerAddress("mongo2.example.com", 27017),
73
new ServerAddress("mongo3.example.com", 27017)
74
);
75
MongoClient client = new MongoClient(seeds);
76
```
77
78
**Connection String (URI) Based:**
79
80
```java
81
// Connect using MongoDB URI
82
MongoClientURI uri = new MongoClientURI("mongodb://user:password@mongo1.example.com:27017,mongo2.example.com:27017/mydb");
83
MongoClient client = new MongoClient(uri);
84
```
85
86
### MongoClient (Modern Interface)
87
88
Modern interface-based client for dependency injection and testing scenarios.
89
90
```java { .api }
91
/**
92
* Modern MongoDB client interface
93
*/
94
public interface MongoClient extends Closeable {
95
MongoDatabase getDatabase(String databaseName);
96
MongoIterable<String> listDatabaseNames();
97
ListDatabasesIterable<Document> listDatabases();
98
ClientSession startSession();
99
ClientSession startSession(ClientSessionOptions options);
100
ChangeStreamIterable<Document> watch();
101
void close();
102
}
103
```
104
105
### MongoClients Factory
106
107
Factory class for creating MongoClient instances with modern configuration patterns.
108
109
```java { .api }
110
/**
111
* Factory for MongoClient instances
112
*/
113
public final class MongoClients {
114
/**
115
* Create a default MongoClient connected to localhost:27017
116
*/
117
public static MongoClient create();
118
119
/**
120
* Create MongoClient from connection string
121
* @param connectionString MongoDB connection string
122
*/
123
public static MongoClient create(String connectionString);
124
125
/**
126
* Create MongoClient with detailed settings
127
* @param settings Client configuration settings
128
*/
129
public static MongoClient create(MongoClientSettings settings);
130
}
131
```
132
133
**Modern Client Creation Examples:**
134
135
```java
136
// Default connection
137
MongoClient client = MongoClients.create();
138
139
// Connection string
140
MongoClient client = MongoClients.create("mongodb://localhost:27017/mydb");
141
142
// With settings
143
MongoClientSettings settings = MongoClientSettings.builder()
144
.applyConnectionString(new ConnectionString("mongodb://localhost:27017"))
145
.readPreference(ReadPreference.secondary())
146
.build();
147
MongoClient client = MongoClients.create(settings);
148
```
149
150
### Server Address Configuration
151
152
Represents MongoDB server addresses for connection configuration.
153
154
```java { .api }
155
/**
156
* Represents a MongoDB server address
157
*/
158
public class ServerAddress {
159
public ServerAddress();
160
public ServerAddress(String host);
161
public ServerAddress(String host, int port);
162
public ServerAddress(InetAddress addr, int port);
163
public ServerAddress(InetSocketAddress addr);
164
165
public String getHost();
166
public int getPort();
167
public boolean isResolved();
168
public InetSocketAddress getSocketAddress();
169
}
170
```
171
172
### Connection URI
173
174
MongoDB connection URI for comprehensive connection configuration.
175
176
```java { .api }
177
/**
178
* MongoDB connection URI parser and configuration
179
*/
180
public class MongoClientURI {
181
public MongoClientURI(String uri);
182
183
public String getURI();
184
public String getDatabase();
185
public String getUsername();
186
public char[] getPassword();
187
public List<String> getHosts();
188
public MongoClientOptions getOptions();
189
public List<MongoCredential> getCredentials();
190
}
191
```
192
193
**Connection URI Format:**
194
195
```
196
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
197
```
198
199
**URI Examples:**
200
201
```java
202
// Basic connection
203
new MongoClientURI("mongodb://localhost:27017/mydb");
204
205
// With authentication
206
new MongoClientURI("mongodb://user:pass@localhost:27017/mydb");
207
208
// Replica set with options
209
new MongoClientURI("mongodb://mongo1:27017,mongo2:27017,mongo3:27017/mydb?replicaSet=myReplSet&readPreference=secondary");
210
```
211
212
## Types
213
214
```java { .api }
215
/**
216
* MongoDB namespace combining database and collection names
217
*/
218
public final class MongoNamespace {
219
public MongoNamespace(String databaseName, String collectionName);
220
public MongoNamespace(String fullName);
221
222
public String getDatabaseName();
223
public String getCollectionName();
224
public String getFullName();
225
}
226
227
/**
228
* Driver information for library authors
229
*/
230
public final class MongoDriverInformation {
231
public static Builder builder();
232
233
public static final class Builder {
234
public Builder driverName(String driverName);
235
public Builder driverVersion(String driverVersion);
236
public Builder driverPlatform(String driverPlatform);
237
public MongoDriverInformation build();
238
}
239
}
240
```