Skip to content

Add configuration properties for MongoDB's read and write consistency #46261

Description

@Torres-09

Summary

Currently, MongoProperties in Spring Boot does not provide a dedicated property for configuring MongoDB read preference. This limits the ability to declaratively set read preference modes and tags via application properties, especially in replica set environments.

Current Workarounds

Using connection URI with readPreference parameter:

spring:
  data:
    mongodb:
      uri: mongodb://localhost:27017/test?readPreference=secondaryPreferred

Or customizing MongoClientSettings via Java configuration

Limitations:

URI-based configuration is less consistent with other MongoDB properties.
Java-based customization increases configuration complexity and reduces clarity for operations teams.

Proposal

Add a readPreference property to MongoProperties, enabling configuration like:

spring:
  data:
    mongodb:
      read-preference: secondaryPreferred

This property would map to the corresponding ReadPreference in the MongoDB Java driver.

Ensure full backward compatibility with existing URI-based configurations.

Optionally support configuration of readPreferenceTags if alignment with advanced use cases is desired.

Use Cases

Applications running on MongoDB replica sets that require secondary or custom read routing.

Environments where configuration consistency and clarity are important (e.g., cloud-native deployments, infrastructure as code).

References

MongoDB Read Preference Documentation

Spring Boot MongoProperties Source

Request

Is there interest in supporting this feature in Spring Boot?

If so, I am willing to contribute a PR with the proposed changes and documentation updates.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions