Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoDB - query by DBRef #367

Open
sondavide opened this issue Mar 11, 2024 · 2 comments
Open

MongoDB - query by DBRef #367

sondavide opened this issue Mar 11, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@sondavide
Copy link

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
Copy link
Member

torshid commented Mar 11, 2024

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

@torshid torshid added the enhancement New feature or request label Mar 11, 2024
@sondavide
Copy link
Author

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"}]}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants