or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

aggregation.mdauthentication-security.mdbson-types.mdchange-streams.mdcollection-operations.mdconfiguration.mdconnection-management.mddatabase-operations.mdgridfs.mdindex.mdquery-building.mdsessions-transactions.md

configuration.mddocs/

0

# Configuration & Options

1

2

Client configuration options for connection pooling, read/write preferences, timeouts, and codec registries to optimize MongoDB driver behavior.

3

4

## Capabilities

5

6

### MongoClientOptions

7

8

```java { .api }

9

public final class MongoClientOptions {

10

public static Builder builder();

11

12

public int getConnectionsPerHost();

13

public int getConnectTimeout();

14

public int getSocketTimeout();

15

public ReadPreference getReadPreference();

16

public WriteConcern getWriteConcern();

17

public ReadConcern getReadConcern();

18

19

public static final class Builder {

20

public Builder connectionsPerHost(int connectionsPerHost);

21

public Builder connectTimeout(int connectTimeout);

22

public Builder socketTimeout(int socketTimeout);

23

public Builder readPreference(ReadPreference readPreference);

24

public Builder writeConcern(WriteConcern writeConcern);

25

public Builder readConcern(ReadConcern readConcern);

26

public MongoClientOptions build();

27

}

28

}

29

30

// Read preferences

31

public abstract class ReadPreference {

32

public static ReadPreference primary();

33

public static ReadPreference secondary();

34

public static ReadPreference secondaryPreferred();

35

public static ReadPreference nearest();

36

public static ReadPreference primaryPreferred();

37

}

38

39

// Write concerns

40

public class WriteConcern {

41

public static final WriteConcern ACKNOWLEDGED;

42

public static final WriteConcern UNACKNOWLEDGED;

43

public static final WriteConcern MAJORITY;

44

public static final WriteConcern W1;

45

public static final WriteConcern W2;

46

public static final WriteConcern W3;

47

48

public WriteConcern(int w);

49

public WriteConcern(String w);

50

public WriteConcern withJournal(Boolean journal);

51

public WriteConcern withWTimeout(long wTimeout, TimeUnit timeUnit);

52

}

53

54

// Read concerns

55

public final class ReadConcern {

56

public static final ReadConcern DEFAULT;

57

public static final ReadConcern LOCAL;

58

public static final ReadConcern AVAILABLE;

59

public static final ReadConcern MAJORITY;

60

public static final ReadConcern LINEARIZABLE;

61

public static final ReadConcern SNAPSHOT;

62

63

public String getLevel();

64

public boolean isServerDefault();

65

}

66

67

// Connection string

68

public class ConnectionString {

69

public ConnectionString(String connectionString);

70

71

public String getConnectionString();

72

public List<String> getHosts();

73

public String getDatabase();

74

public String getCollection();

75

public String getUsername();

76

public char[] getPassword();

77

public ReadPreference getReadPreference();

78

public WriteConcern getWriteConcern();

79

public ReadConcern getReadConcern();

80

public String getApplicationName();

81

}

82

83

// Driver information for libraries

84

public final class MongoDriverInformation {

85

public static Builder builder();

86

public static Builder builder(MongoDriverInformation mongoDriverInformation);

87

88

public static final class Builder {

89

public Builder driverName(String driverName);

90

public Builder driverVersion(String driverVersion);

91

public Builder driverPlatform(String driverPlatform);

92

public MongoDriverInformation build();

93

}

94

}

95

96

// Modern client settings (recommended)

97

public final class MongoClientSettings {

98

public static Builder builder();

99

100

public ReadPreference getReadPreference();

101

public WriteConcern getWriteConcern();

102

public ReadConcern getReadConcern();

103

public CodecRegistry getCodecRegistry();

104

public String getApplicationName();

105

106

public static final class Builder {

107

public Builder applyConnectionString(ConnectionString connectionString);

108

public Builder readPreference(ReadPreference readPreference);

109

public Builder writeConcern(WriteConcern writeConcern);

110

public Builder readConcern(ReadConcern readConcern);

111

public Builder codecRegistry(CodecRegistry codecRegistry);

112

public Builder applicationName(String applicationName);

113

public MongoClientSettings build();

114

}

115

}

116

```

117

118

### Connection String Configuration

119

120

Connection strings provide a convenient way to configure MongoDB client settings including authentication, read preferences, and connection options.

121

122

**Usage Examples:**

123

124

```java

125

import com.mongodb.ConnectionString;

126

import com.mongodb.client.MongoClients;

127

import com.mongodb.client.MongoClient;

128

129

// Basic connection

130

ConnectionString connString = new ConnectionString("mongodb://localhost:27017/mydb");

131

MongoClient client = MongoClients.create(connString);

132

133

// With authentication and options

134

ConnectionString authConnString = new ConnectionString(

135

"mongodb://user:password@localhost:27017/mydb?authSource=admin&readPreference=secondary"

136

);

137

MongoClient authClient = MongoClients.create(authConnString);

138

139

// Replica set connection

140

ConnectionString replicaString = new ConnectionString(

141

"mongodb://host1:27017,host2:27017,host3:27017/mydb?replicaSet=myReplSet"

142

);

143

MongoClient replicaClient = MongoClients.create(replicaString);

144

```

145

146

### Client Settings Configuration

147

148

Programmatic configuration using MongoClientSettings for more complex setups.

149

150

**Usage Examples:**

151

152

```java

153

import com.mongodb.MongoClientSettings;

154

import com.mongodb.ReadPreference;

155

import com.mongodb.WriteConcern;

156

import java.util.concurrent.TimeUnit;

157

158

MongoClientSettings settings = MongoClientSettings.builder()

159

.applyConnectionString(new ConnectionString("mongodb://localhost:27017"))

160

.readPreference(ReadPreference.secondaryPreferred())

161

.writeConcern(WriteConcern.MAJORITY.withWTimeout(5, TimeUnit.SECONDS))

162

.applicationName("MyApplication")

163

.build();

164

165

MongoClient client = MongoClients.create(settings);

166

```