spring-projects / spring-projects/spring-data-mongodb

Explore if it makes sense to provide an EvaluationContextExtension supports optional/null query parameters.

Open
#4,050 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

type: enhancement
Dominant language
Java
Stars
1.7k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

Using @Query to skip entire criteria entries when given a null value is cumbersome at the moment but could be done as follows.

interface Repo extends CrudRepository<Person,...> {
  @Query("""
         { $and : [ 
            ?#{T(com.example.Repo.QueryUtil).ifPresent([0], 'name')}, 
            ?#{T(com.example.Repo.QueryUtil).ifPresent([1], 'city')},
            ... 
         ]}
         """)
  List<Person> findByNameAndCity(@Nullable String name, @Nullable String city, ...);

  class QueryUtil {
    public static Document ifPresent(Object value, String property) {
      if(value == null) {
        return new Document("$expr", true); // always true
      }
      return new Document(property, value); // eq match
    }
  }
  // ...
}

Providing an EvaluationContextExtension with predefined methods might help ease a lot of pain.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the @Query usage and the EvaluationContextExtension mechanism described in the issue. Determine whether predefined methods can support optional or null query parameters, and document the resulting design or implementation scope; the issue names no files or tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, mongodb, spring
Domain
backend, databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.