Configure Client-level CRUD Settings
On this page
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: |
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 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.