turkraft / springfilter

Dynamically filter JPA entities and Mongo collections with a user-friendly query syntax. Seamless integration with Spring APIs. Star to support the project! ⭐️
https://turkraft.com/springfilter
216 stars 35 forks source link

MongoDB - query by DBRef #367

Open sondavide opened 6 months ago

sondavide commented 6 months ago

Hi, I'm using this version:

com.turkraft.springfilter mongo 3.1.7

there's a way to create a filter for a DBRef property?

For example, if I have this class:

class User{ String name; String surname; ... @DBRef private List role; }

how can I do for example a query by role.$id = "642ebb0e91ac8f778f5654b7" ?

torshid commented 6 months ago

This is not possible unfortunately. It requires complex Mongo queries in the backend.

sondavide commented 6 months ago

Ok, the cleanest workaround is to add two parameter in the controller:

@GetMapping("/search")
public ResponseEntity<Page<UserDto>> search(@Filter(entityClass = User.class) Query query, Pageable pag, @RequestParam(required = false)List<String> role){
    if(role!=null && role.size()>0)
         query.addCriteria(Criteria.where("role.$id").in(role.stream().map(it-> new ObjectId(it)).toList()));
    return ResponseEntity.ok(userService.search(query, pag).map(it -> new UserDto(it)));
}

this kind of query works for example:

{ "role.$id" : { "$in" : [{ "$oid" : "6411a9fe93dc05fff53666c4"}]}}