How to disable spring-data-mongodb autoconfiguration in spring-boot
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Spring Boot will auto-configure MongoDB as soon as the right classes appear on the classpath and it believes a Mongo setup is possible. If a module includes the Mongo starter only transitively, or if certain profiles should run without a database, you need to stop that auto-configuration explicitly.
What Boot is auto-configuring
When spring-boot-starter-data-mongodb is present, Boot can create beans such as:
- '
MongoClient' - '
MongoTemplate' - Mongo repository infrastructure
That is convenient when the application really uses MongoDB. It becomes a problem when:
- tests should start without a database
- a shared starter brings Mongo in by accident
- one runtime profile uses Mongo and another does not
Disabling the auto-configuration prevents Boot from trying to create those beans during startup.
Excluding Mongo auto-configuration in code
The most direct fix is to exclude the relevant Boot classes on your application entry point:
This works well when Mongo should be disabled everywhere in that application.
The two exclusions matter for different reasons. One covers client creation, and the other covers Spring Data integration. Excluding only one of them usually leads to half-configured behavior.
Disabling it from configuration
If you want the decision to be environment-specific, use configuration instead of annotations:
The YAML form is often easier to read:
This is useful in application-test.yml or another profile-specific file where Mongo should stay off.
Turning Mongo on only when you mean to
Some teams prefer to disable Boot’s Mongo setup and register their own configuration only behind a custom flag:
Then a profile can opt in:
This pattern makes the dependency explicit instead of accidental.
Verifying that the exclusion worked
The fastest manual check is startup logging with Boot’s debug report:
You can also write a small test:
The point is not just to silence an error. The point is to prove the application context matches the intended architecture.
Common Pitfalls
One common mistake is excluding only MongoAutoConfiguration and leaving MongoDataAutoConfiguration enabled. Spring Data then still tries to wire Mongo-related infrastructure and startup can fail in confusing ways.
Another issue is forgetting about reactive Mongo support. If the reactive starter is on the classpath, it has its own auto-configuration classes and may need separate exclusion.
Teams also get caught by explicit annotations such as @EnableMongoRepositories. If you add that manually anywhere, Boot exclusions alone may not be enough because repository scanning has been reintroduced deliberately.
Finally, check the dependency tree. If Mongo came from a transitive dependency and you do not need it at all, excluding that dependency upstream may be cleaner than disabling Boot behavior downstream.
Summary
- Spring Boot enables Mongo support automatically when the starter is on the classpath.
- Exclude both
MongoAutoConfigurationandMongoDataAutoConfigurationwhen you want it off. - Use annotation-based exclusion for a global decision and configuration-based exclusion for profile-specific behavior.
- Prefer explicit custom configuration if Mongo should exist only behind a feature flag.
- Verify the result with startup diagnostics or a context test instead of assuming the exclusion worked.
Related reading
- How to do a batch insert in MySQL
- How to do a regular expression replace in MySQL?
- How to do an update + join in PostgreSQL?
- How to do binary search by table with known data order in specific fields SQL
- How to disable spring-security login screen?
- How to disable spring boot logo in stdout?
- How to do bulk multi row inserts with JpaRepository?
- How to do Query in DynamoDB on the basis of HashKey and range Key?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.