spring-projects / spring-projects/spring-boot
Add configuration properties for MongoDB's read and write consistency
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 81.5k
- Forks
- 42.7k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 65
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with spring-boot-project/spring-boot-mongodb/src/main/java/org/springframework/boot/mongodb/autoconfigure/MongoProperties.java, then review the related MongoDB configuration and documentation. Define the property behavior, including compatibility with URI-based settings and whether read preference tags are in scope; done means the property can configure read preference declaratively and the documentation reflects the supported configuration.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, mongodb, spring-boot
- Domain
- backend, databases
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100