Overview
In this guide, you can learn about how to use the Java driver to configure CRUD operations for MongoClient instances.
Read preferences, read concerns, and write concerns control how the driver routes read operations and waits for acknowledgment for read and write operations when connected to a MongoDB replica set. Read preferences and read concerns apply to all read operations; write concerns apply to all write operations.
For more information, see the server documentation on read preferences, read concerns, and write concerns.
MongoDatabase and MongoCollection instances inherit their preferences and concerns from the MongoClient that accesses them. However, you can apply custom settings to your databases and collections. For more information, see the Configure Custom CRUD Settings page.
You can specify client-level CRUD settings by using either a connection
string or by passing a MongoClientSettings object to the MongoClients constructor. Select the Connection String or MongoClientSettings tab to see the options available:
Include the following parameters in your connection string to modify the driver's read or write behavior:
Option Name | Type | Description |
|---|---|---|
| boolean | Specifies that the driver must wait for the connected MongoDB instance to group commit to the journal file on disk for all writes. Default: |
| string or integer | Specifies the write concern. For more information about values, see the MongoDB Server documentation for the w option. Default: |
| integer | Specifies a time limit, in milliseconds, for the write concern. For more information, see the MongoDB Server documentation for the wtimeoutMS option. A value of Default: |
| string | Specifies the read preference. For more information about values, see the MongoDB Server documentation for the readPreference option. Default: |
| string | Specifies the read preference tags. For more information about values, see the MongoDB Server documentation for the readPreferenceTags option. Default: |
| integer | Specifies, in seconds, how stale a secondary can be before the driver stops communicating with that secondary. The minimum value is either 90 seconds or the heartbeat frequency plus 10 seconds, whichever is greater. For more information, see the MongoDB Server documentation for the maxStalenessSeconds option. Not providing a parameter or explicitly specifying Default: |
| string | Specifies the UUID representation to use for read and write operations. For more information, see the driver documentation for the MongoClientSettings.getUuidRepresentation() method. Default: |
| boolean | Specifies that the driver must retry supported write operations if they are unable to complete due to a network error. Default: |
| boolean | Specifies that the driver must retry supported read operations if they are unable to complete due to a network error. Default: |
| integer | Sets the maximum number of times the driver retries a command that fails with a retryable overload error (that is, an error that carries both the This option applies to write commands only when Default: |
| boolean | Sets whether the driver may deprioritize servers during server selection for subsequent retry attempts if they previously returned overload errors. This option applies to reads when Default: |
The following example sets the read preference to read from the nearest replica set member:
ConnectionString connectionString = "mongodb://<host>:<port>/?readPreference=nearest" MongoClient mongoClient = MongoClients.create(connectionString)
For more information about these parameters, see the ConnectionString API documentation.
Chain the following methods to your MongoClientSettings constructor to modify the driver's read/write behavior:
| Sets whether the driver may deprioritize servers during server selection for subsequent retry attempts if they previously returned overload errors. Applies to reads when Default: |
| Sets the maximum number of times the driver retries a command that fails with a retryable overload error (that is, an error that carries both the Default: |
| Sets the read concern. Server manual page API documentation |
| Sets the read preference Default: |
| Whether the driver performs retry reads if a network error occurs. Default: |
| Whether the driver performs retry writes if a network error occurs. Default: |
| Sets the UUID representation to use when encoding instances of UUID and decoding BSON binary values with subtype of 3. |
| Sets the write concern. Default: |
The following example sets the read preference to read from the nearest replica set member:
MongoClient mongoClient = MongoClients.create( MongoClientSettings.builder().applyConnectionString(new ConnectionString("<your connection string>")) .readPreference(ReadPreference.nearest()) .build());
For more information about these methods, see the MongoClientSettings.Builder API documentation.
Note
Load Shedding in Intelligent Workload Management (IWM)
If errors with SystemOverloadedError or RetryableError labels are causing failures in your application or appearing in your application logs, Load Shedding may be active on your cluster and your driver may not be upgraded to a version that supports it. We recommend upgrading your MongoDB Java Driver to version 5.8 or later. If you continue to see these errors after upgrading, you can address them through server-side changes, such as reviewing your Load Shedding configuration, or through application-side changes, such as implementing custom error handling or client-side request throttling. Depending on your situation, application-side changes may be preferable or necessary.
Load Shedding is a feature of IWM, and helps manage server load by rejecting excess operations during sustained overload. This feature is available on MongoDB 9.0 and later.
For more information about IWM or handling overload errors, see the Intelligent Workload Management page or Overload Errors page in the Atlas documentation.