> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

<!--
Tab options on this page. Append to the .md URL to filter:
  ?tabs=<id,...>   select specific tabs (e.g. ?tabs=nodejs,shell)
  ?allTabs=true    include every tab
  (no param)       default: one tab per tabset

Available tabs:
  other tabs: uri, MongoClient
-->

# Configure Client-level CRUD Settings

## 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](https://www.mongodb.com/docs/manual/core/read-preference/), [read concerns](https://www.mongodb.com/docs/manual/reference/read-concern/), and [write concerns.](https://www.mongodb.com/docs/manual/reference/write-concern/)

`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](https://www.mongodb.com/docs/drivers/java/sync/current/crud/crud-settings.md#std-label-java-configure-custom-crud) page.

You can specify client-level CRUD settings by using either a [connection string](https://www.mongodb.com/docs/drivers/java/sync/current/connection/mongoclient.md#std-label-connection-uri) or by passing a `MongoClientSettings` object to the [MongoClients](https://www.mongodb.com/docs/drivers/java/sync/current/connection/mongoclient.md#std-label-mongoclient) constructor. Select the Connection String or MongoClientSettings tab to see the options available:

### Connection String

Include the following parameters in your connection string to modify the driver's read or write behavior:

| Option Name | Type | Description |
| --- | --- | --- |
| `journal` | 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*: `false` |
| `w` | string or integer | Specifies the write concern. For more information about values, see the MongoDB Server documentation for the [w option.](https://www.mongodb.com/docs/manual/reference/write-concern/#w-option) *Default*: `1` |
| `wtimeoutMS` | integer | Specifies a time limit, in milliseconds, for the write concern. For more information, see the MongoDB Server documentation for the [wtimeoutMS option](https://www.mongodb.com/docs/manual/reference/connection-string/#write-concern-options). A value of `0` instructs the driver to never time out write operations. *Default*: `0` |
| `readPreference` | string | Specifies the read preference. For more information about values, see the MongoDB Server documentation for the [readPreference option.](https://www.mongodb.com/docs/manual/reference/connection-string/#urioption.readPreference) *Default*: `primary` |
| `readPreferenceTags` | string | Specifies the read preference tags. For more information about values, see the MongoDB Server documentation for the [readPreferenceTags option.](https://www.mongodb.com/docs/manual/reference/connection-string/#urioption.readPreferenceTags) *Default*: `null` |
| `maxStalenessSeconds` | 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](https://www.mongodb.com/docs/manual/reference/connection-string/#urioption.maxStalenessSeconds). Not providing a parameter or explicitly specifying `-1` indicates that there must be no staleness check for secondaries. *Default*: `-1` |
| `uuidRepresentation` | string | Specifies the UUID representation to use for read and write operations. For more information, see the driver documentation for the [MongoClientSettings.getUuidRepresentation() method.](https://mongodb.github.io/mongo-java-driver/5.13/apidocs/driver-core/com/mongodb/MongoClientSettings.html#getUuidRepresentation()) *Default*: `unspecified` |
| `retryWrites` | boolean | Specifies that the driver must retry supported write operations if they are unable to complete due to a network error. *Default*: `true` |
| `retryReads` | boolean | Specifies that the driver must retry supported read operations if they are unable to complete due to a network error. *Default*: `true` |
| `maxAdaptiveRetries` | 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 `SystemOverloadedError` and `RetryableError` labels). This option applies to write commands only when `retryWrites` is set to `true` and read commands only when `retryReads` is set to `true`. Setting the value to `0` disables retry attempts for retryable overload errors. *Default*: `null`. When this option is not set, the driver typically applies the value `2`. |
| `enableOverloadRetargeting` | 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 `retryReads` is set to `true` and to writes when `retryWrites` is set to `true`. This option does not apply to sharded clusters. *Default*: `false` |

The following example sets the read preference to read from the nearest replica set member:

```java
ConnectionString connectionString = "mongodb://<host>:<port>/?readPreference=nearest"
MongoClient mongoClient = MongoClients.create(connectionString)
```

For more information about these parameters, see the [ConnectionString](https://mongodb.github.io/mongo-java-driver/5.13/apidocs/driver-core/com/mongodb/ConnectionString.html) 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](https://www.mongodb.com/docs/atlas/intelligent-workload-management/) page or [Overload Errors](https://www.mongodb.com/docs/atlas/overload-errors/?interface=driver\&language=|language|) page in the Atlas documentation.
