Skip to content

Support IsEmpty and IsNotEmpty keywords in derived queries#5147

Closed
backend-choijunhyeong wants to merge 1 commit intospring-projects:mainfrom
backend-choijunhyeong:gh-4606-is-empty-keyword
Closed

Support IsEmpty and IsNotEmpty keywords in derived queries#5147
backend-choijunhyeong wants to merge 1 commit intospring-projects:mainfrom
backend-choijunhyeong:gh-4606-is-empty-keyword

Conversation

@backend-choijunhyeong
Copy link
Contributor

Add support for IsEmpty and IsNotEmpty repository query keywords in derived queries.

Why

The documentation lists IsEmpty as a supported keyword, but it throws
"Unsupported keyword" exception when used.

Changes

  • Add IS_EMPTY and IS_NOT_EMPTY case handling in MongoQueryCreator.from()
  • Add createIsEmptyCriteria() and createIsNotEmptyCriteria() methods
  • String properties: { field: { $eq: "" } } / { field: { $ne: "" } }
  • Collection properties: { field: { $size: 0 } } / { field: { $not: { $size: 0 } } }

Resolves #4606

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 18, 2026
@backend-choijunhyeong
Copy link
Contributor Author

backend-choijunhyeong commented Jan 18, 2026

I'd like to clarify the expected behavior regarding null values.

Current implementation

  • IsEmpty: matches only "" (empty string) or size: 0 (empty collection)
  • IsNotEmpty: matches anything that is not "" or size: 0

This means null values are NOT considered "empty".

Question
Should IsEmpty also match null values?
For example: findByFirstNameIsEmpty -> should it return documents where firstName is null?

Options

  1. Current: null is NOT considered empty (strict interpretation)
  2. Alternative: null OR "" is considered empty (lenient interpretation)

I can adjust the implementation based on the team's preference.

@mp911de mp911de added status: pending-design-work Needs design work before any code can be developed type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Jan 26, 2026
@christophstrobl
Copy link
Member

Thank you @backend-choijunhyeong for putting time into this.

I think the handling of String and Collection like properties is fine. There are dedicated keywords for null checks, so using $eq/$ne and $size works well.
It might be worth mentioning that blank String values (containing only whitespaces) should also not be considered empty (which is already the case).

Map like structures are not covered yet and should IMO be compared against an empty Document. Same goes for complex/domain types like an Address.

Having some integration tests in AbstractPersonRepositoryIntegrationTests would be good. This allows to verify the queries behave as expected and in the same way througout different scenarios (like for AOT generated code).

Let me know if that helps and if you'd like to continue working on the issue or if we should take it from here.

@backend-choijunhyeong
Copy link
Contributor Author

Thank you for the feedback @christophstrobl !

I'd like to continue working on this.

I will

  • Add support for Map and complex/domain types by comparing against an empty Document
  • Add integration tests in AbstractPersonRepositoryIntegrationTests

I'll update the PR once the changes are ready.

@backend-choijunhyeong
Copy link
Contributor Author

@christophstrobl

I have updated the implementation to address your feedback

  • Added support for Map and complex types (compares with an empty document {}).
  • Added integration tests for String, Collection, Map, and domain types.

Question
Regarding your mention that "Numeric values should error," should I add explicit validation to throw an exception for numeric types, or is the current behavior acceptable?

Thank you for the review.

Add IS_EMPTY and IS_NOT_EMPTY case handling to MongoQueryCreator.
For String properties, compare with empty string using $eq/$ne.
For Collection properties, use $size operator.
For Map and other types, compare with empty document.

Signed-off-by: backend-choijunhyeong <ajfeh22@gmail.com>
christophstrobl pushed a commit that referenced this pull request Mar 2, 2026
Add IS_EMPTY and IS_NOT_EMPTY case handling to MongoQueryCreator.
For String properties, compare with empty string using $eq/$ne.
For Collection properties, use $size operator.
For Map and other types, compare with empty document.

Closes: #4606
Original Pull Request: #5147

Signed-off-by: backend-choijunhyeong <ajfeh22@gmail.com>
christophstrobl added a commit that referenced this pull request Mar 2, 2026
Simplify code flow by using newly introduced Criteria.eq().
Make sure to consider a potential explicit field target type when creating empty check.

Original Pull Request: #5147
@christophstrobl
Copy link
Member

Thank you for your contribution @backend-choijunhyeong . The changes have been merged to the main development line via 573d416.
Regarding your question about numeric types, I'd keep things as designed for now and wait for additional feedback on the issue tracker (if any) on reasons why an empty condition on a numeric field makes sense in first place.

@christophstrobl christophstrobl removed the status: pending-design-work Needs design work before any code can be developed label Mar 2, 2026
@christophstrobl christophstrobl added this to the 5.1 M2 (2026.0.0) milestone Mar 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: enhancement A general enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Repository query keyword IS_EMPTY not supported

4 participants