Spring Data MongoDB Repository Queries
Fast recall
Quick revision
Core answer
findByStatusAndCreatedAtAfter). `@Query` annotations take raw MQL JSON for anything the derived parser can't express ($or, $elemMatch, nested paths). `MongoTemplate` with the `Criteria` API builds queries programmatically for dynamic search forms. The `Aggregation` API handles pipelines ($group, $lookup, $project) that the repository layer can't. For reactive apps, swap MongoRepository for ReactiveMongoRepository to return Flux and Mono. The skill is knowing when to step down the stack rather than contorting a derived method into something illegible.Whether you know the full query toolkit in Spring Data MongoDB and can match mechanism to complexity
Using derived methods for every query, leading to 80-character method names; or dropping to `MongoTemplate` for simple queries that a derived method would cover
Show the Criteria API for dynamic forms, `MongoTemplate.aggregate()` for pipelines, projection interfaces, and reactive repositories for WebFlux
Interview-ready guide
Interview answer
Describe the four levels
- Derived methods — method-name parsing for simple filters
@Query— raw MQL JSON for$or,$elemMatch, nested paths- Criteria +
MongoTemplate— dynamic queries and conditional predicates Aggregation— pipelines for$group,$lookup,$project,$facet
Give the step-down rule
- Start with derived; step up only when it becomes illegible or impossible
- Reach for Criteria when predicates depend on runtime input
- Reach for Aggregation when the query is pipeline-shaped
Mention reactive
ReactiveMongoRepository+ReactiveMongoTemplate- Non-blocking on Netty
Close with transactions
MongoTransactionManager+@Transactional- Requires replica set
Spring Data MongoDB Repository Queries
Direct Answer
Spring Data MongoDB offers four query mechanisms that step up in power and complexity. Derived query methods let you express simple filters via method names (findByStatusAndCreatedAtAfter). @Query annotations take raw MQL JSON for anything the derived parser can't express ($or, $elemMatch, nested paths). MongoTemplate with the Criteria API builds queries programmatically for dynamic search forms. The Aggregation API handles pipelines ($group, $lookup, $project) that the repository layer can't. For reactive apps, swap MongoRepository for ReactiveMongoRepository to return Flux and Mono. The skill is knowing when to step down the stack rather than contorting a derived method into something illegible.
Common Mistakes & Pitfalls
Using derived methods for every query, leading to 80-character method names; or dropping to MongoTemplate for simple queries that a derived method would cover
Check your understanding
Answer these without looking back at the article.
- When would you use a Spring Data projection interface instead of a DTO?
- How do you wire a `MongoTransactionManager` for multi-document transactions?
- What does `ReactiveMongoRepository` give you that the blocking variant doesn't?
- When does `$lookup` in an aggregation beat a second repository call?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.