In Spring Data MongoDB, @Query lets you write a MongoDB JSON query by hand and attach it to a repository method. Spring replaces placeholders such as ?0 with the method arguments, runs the query and maps the documents to your class. It is the right choice when a derived method name cannot express the filter, or when you want to control projection, sorting or the query type explicitly.
The annotation is org.springframework.data.mongodb.repository.Query.
@Query does in a MongoDB repository@Query is not enough@Query does in a MongoDB repositoryBy default a method such as findByStatusAndPriceGreaterThan is converted into a MongoDB query from its name. That works for simple conditions. @Query replaces the derived query with the JSON filter you write, in the same syntax as the find() command of mongosh. Operators such as $gte, $in, $regex, $or and $elemMatch are available directly.
Placeholders are zero-based: ?0 is the first method argument and ?1 the second. The first method filters by category and minimum price. The second runs a case-insensitive regular expression; because patterns can be expensive on large collections, back them with an index or consider a text or Atlas Search index.
@Query has attributes that change what is returned.
fields is a projection: 1 includes a field and 0 excludes it. count, exists and delete switch the method from reading documents to counting, checking or removing them. A delete = true method changes data, so keep its filter narrow and tested.
Add a Pageable parameter and return Page or Slice, and Spring applies skip, limit and sort for you:
You can also use SpEL for values computed at call time, for example reading a property of an argument:
Pass user input as arguments, never by concatenating it into the JSON. Spring Data binds arguments as values, which prevents a user from injecting extra operators through a string.
@Query is not enough| Need | Option |
|---|---|
| Simple filters | Derived query methods |
| Fixed JSON filter, projection or sort | @Query |
| Aggregation pipelines (group, lookup, unwind) | @Aggregation on the repository method |
| Filters built at runtime | MongoTemplate with Criteria and Query |
An @Query string is static. If the combination of filters changes with each request, build a Criteria in code instead of writing many annotated variants.
@Query do joins in MongoDB?Not directly. A find query reads one collection. Use @Aggregation with $lookup, or MongoTemplate, when you need data from another collection.
?0 zero-based?Yes. Placeholders are indexed from zero, so ?0 is the first parameter of the method.
@Query support transactions?Transactions are a separate concern. Multi-document transactions need a replica set or sharded cluster and a transaction manager, and you apply them with @Transactional; the query annotation works the same inside or outside a transaction.
@Query from?Use org.springframework.data.mongodb.repository.Query. The JPA and Elasticsearch modules have annotations with the same simple name.
@Query in Spring Data MongoDB turns a repository method into a hand-written MongoDB filter with positional placeholders. Use fields, sort, count, exists and delete to shape the result, Pageable for paging, @Aggregation for pipelines, and MongoTemplate when the query must be built dynamically.