Thursday, 29 August 2019

springboot monogo query -- repository


you can find build in functions:

https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongodb.repositories.queries



if you just query by fields or fields from sub object, then very simple.

but if you want to query by multi fields.... then the build in functions doesn't help.

but you still can use repository by:

use the Query annotation:

https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongodb.repositories.queries.json-based


you can put a monogo query here:


@Query(value = "{ $and: [ { 'sex' : ?0 }, { 'name' :?1 } ] }")
    public List<Users> findUsersBySexandnamelalalal(String sex, String name);




1. the function name can be anything.
2. you can have multi parameters in the function and use them in the query by ?0, ?1, ?2 ....
3. query value is a monogo query.


No comments:

Post a comment