Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Query named parameters for MongoDb Spring repository

Is it possible to use named parameters for a @Query method in a mongodb repository, just like we can do with a jpa repository (http://docs.spring.io/spring-data/jpa/docs/1.4.3.RELEASE/reference/html/jpa.repositories.html section 2.3.5)?

As an example, I would like to use the following code:

@Query("{'store' : :store, 'app' : :app }")
List<T> findByStoreAndApp(@Param("store") String store, @Param("app") String app);

instead of:

@Query("{'store' : ?0, 'app' : ?1 }")
List<T> findByStoreAndApp(String store, String app);
like image 389
pernacentus Avatar asked Oct 30 '25 14:10

pernacentus


1 Answers

It is possible, try:

@Query("{'store' : :#{#store}, 'app' : :#{#app} }")
List<T> findByStoreAndApp(@Param("store") String store, @Param("app") String app);

The support of SpEL expressions in @Query was introduced in Spring Data MongoDB 1.8.

https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongo.aggregation.projection.expressions

like image 119
Matthias Baumgart Avatar answered Nov 01 '25 12:11

Matthias Baumgart



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!