Skip to content

Support derived queries using the same field multiple times - #5243

Open
anjeongkyun wants to merge 2 commits into
spring-projects:mainfrom
anjeongkyun:gh-4715-derived-query-duplicate-property
Open

anjeongkyun wants to merge 2 commits into
spring-projects:mainfrom
anjeongkyun:gh-4715-derived-query-duplicate-property

Conversation

@anjeongkyun

Copy link
Copy Markdown

A derived query that constrains the same property twice fails today:

List<Person> findByAgeGreaterThanAndAgeLessThan(int lower, int upper);
InvalidMongoDbApiUsageException: Due to limitations of the org.bson.Document, you can't add a second
'age' expression specified as 'age : Document{{$lt=50}}'; Criteria already contains 'age : Document{{$gt=10}}'

MongoQueryCreator#and sends every part through base.and(dotPath), so the second part on age is added to the same document and the chain cannot be rendered.

This adds one branch there: when the property is already constrained in the current chain, the parts are combined with $and.

findByAgeGreaterThanAndAgeLessThan   -> {"$and": [{"age": {"$gt": 10}}, {"age": {"$lt": 50}}]}
findByAgeAndAge                      -> {"$and": [{"age": 10}, {"age": 50}]}
findByFirstNameAndAgeGreaterThanAndAgeLessThan
                                     -> {"$and": [{"firstName": "Oliver", "age": {"$gt": 10}}, {"age": {"$lt": 50}}]}

Queries without a repeated property keep their current single-document shape, and $or branches are unaffected, since each branch carries its own chain. Both are covered by tests.

The Criteria API is not touched. Rejecting a duplicate key there was called intentional in #4715, so this stays on the derivation side; CriteriaUnitTests and QueryTests pass unchanged.

One thing worth flagging: with three parts on the same property the result is {"$and": [A, B], "age": C} rather than a flat {"$and": [A, B, C]}. It is valid and all three conditions are kept, but I can flatten it if you would rather have the tidier shape.

Tests run: MongoQueryCreatorUnitTests (68), plus CriteriaUnitTests, CriteriaTests, QueryTests, QueryMapperUnitTests, PartTreeMongoQueryUnitTests and the other query suites (388 total), and the AOT repository tests (183). The five new tests fail without the change.

Closes #4715

A derived query that constrains one property twice, such as
findByAgeGreaterThanAndAgeLessThan, failed because both conditions were
written into a single document. Combine them with $and instead.

Closes spring-projects#4715

Signed-off-by: anjeongkyun <anwjdrbs123@gmail.com>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Sep 13, 2026
Missed from the PR checklist.

Signed-off-by: anjeongkyun <anwjdrbs123@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: waiting-for-triage An issue we've not yet triaged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support derived queries using the same field multiple times

2 participants