Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs Menu
Docs Home
/ /

Configure CRUD Operations

In this guide, you can learn how to configure read and write operations in the Rust driver.

You can control how the driver routes read operations by setting a read preference. You can also control options for how the driver waits for acknowledgment of read and write operations on a replica set by setting a read concern and a write concern.

By default, databases inherit these settings from the Client instance, and collections inherit them from the database. However, you can set custom read or write settings for a database or collection by using the database_with_options() or collection_with_options() method. These methods create a new Database or Collection instance with the specified settings.

To configure read or write settings, create an instance of the ReadConcern, WriteConcern, or SelectionCriteria type and pass them to the options builder.

Tip

To learn more about the read and write settings, see the following guides in the MongoDB Server manual:

The following sections show how to configure your read and write settings at the client, database, and collection levels.

This example shows how to set the read preference, read concern, and write concern of a Client instance by passing a ClientOptions object to the Client::with_options() method. The code configures the following settings:

  • Secondary read preference: Read operations retrieve data from secondary replica set members

  • local read concern: Read operations return the instance's most recent data without guaranteeing that the data has been written to a majority of the replica set members

  • Majority write concern: The majority of all replica set members must acknowledge the write operation

let read_preference = ReadPreference::Secondary {
options: Default::default(),
};
let selection_criteria = SelectionCriteria::ReadPreference(read_preference);
let read_concern = ReadConcern::local();
let write_concern = WriteConcern::builder().w(Acknowledgment::Majority).build();
let client_options = ClientOptions::builder()
.selection_criteria(selection_criteria)
.read_concern(read_concern)
.write_concern(write_concern)
.build();
let client = Client::with_options(client_options)?;

Alternatively, you can specify the read and write settings in the connection URI, then pass the URI to the Client::with_uri_str() constructor as shown in the following example:

let uri = "<connection string>/?readPreference=secondary&readConcernLevel=local&w=majority";
let client = Client::with_uri_str(uri).await?;

This example shows how to set the read preference, read concern, and write concern of a database named test_database by passing a DatabaseOptions object to the database_with_options() method. The code configures the following settings:

  • PrimaryPreferred read preference: Read operations retrieve data from the primary replica set member, or secondary members if the primary is unavailable

  • available read concern: Read operations return the instance's most recent data without guaranteeing that the data has been written to a majority of the replica set members

  • majority write concern: The majority of all replica set members must acknowledge the write operation

let read_preference = ReadPreference::PrimaryPreferred {
options: Default::default(),
};
let selection_criteria = SelectionCriteria::ReadPreference(read_preference);
let read_concern = ReadConcern::available();
let write_concern = WriteConcern::majority();
let database_options = DatabaseOptions::builder()
.selection_criteria(selection_criteria)
.read_concern(read_concern)
.write_concern(write_concern)
.build();
let database = client.database_with_options("test_database", database_options);

This example shows how to set the read preference, read concern, and write concern of a collection named test_collection by passing a CollectionOptions object to the collection_with_options() method. The code configures the following settings:

  • SecondaryPreferred read preference: Read operations retrieve data from secondary replica set members, or the primary member if no secondaries are available

  • available read concern: Read operations return the instance's most recent data without guaranteeing that the data has been written to a majority of the replica set members

  • w: 0 write concern: The client does not request acknowledgment of the write operation

let read_preference = ReadPreference::SecondaryPreferred {
options: Default::default(),
};
let selection_criteria = SelectionCriteria::ReadPreference(read_preference);
let read_concern = ReadConcern::available();
let write_concern = WriteConcern::builder().w(0.into()).build();
let collection_options = CollectionOptions::builder()
.selection_criteria(selection_criteria)
.read_concern(read_concern)
.write_concern(write_concern)
.build();
let collection = database.collection_with_options("test_collection", collection_options);

The Rust driver automatically retries certain read and write operations one time if they fail due to a network or server error.

You can explicitly disable retryable reads or retryable writes by setting the retry_reads or retry_writes option to false when building a ClientOptions instance. The following example disables retryable reads and writes for a client:

let client_options = ClientOptions::builder()
.retry_reads(false)
.retry_writes(false)
.build();
let client = Client::with_options(client_options)?;

To learn more about supported retryable read operations, see Retryable Reads in the MongoDB Server manual. To learn more about supported retryable write operations, see Retryable Writes in the MongoDB Server manual.

You can specify a collation to modify the behavior of read and write operations. A collation is a set of language-specific rules for string comparison, such as for letter case and accent marks.

For more information about collations and to view code examples, see the Collations guide.

To learn more about any of the methods or types discussed in this guide, see the following API documentation:

Back

Compound Operations

On this page