Add support for optional query parameters in repository methods [DATAMONGO-2486] #3340
Labels
in: repository
Repositories abstraction
status: duplicate
A duplicate of another issue
type: enhancement
A general enhancement
shollander opened DATAMONGO-2486 and commented
I would be nice to be have optional parameters for a repository method/query. For example:
If
firstname
contained a value then it would be included in the query. It it was empty then it would not be included in the query.For example:
Would generate the following query:
While
Would generate the following query:
This is a simple example, but the value of this feature is realized when you have a query that contains many optional parameters. Creating all possible permutations can get very complicated. I believe having Optional would greatly simplify many use cases.
Note that while
Optional
can currently be used in a repository method parameter, it does not behave as expected. The current behavior is to query for a null value when passed Optional.empty(). I believe this is counter-intuitive since Spring Data MongoDB does not normally insert null fields in the database. If a field has a null value, that field does not exist in the mongo documentIssue Links:
("duplicates")
("duplicates")
The text was updated successfully, but these errors were encountered: