spring-projects / spring-projects/spring-data-mongodb
Support derived queries using the same field multiple times
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 1.7k
- Forks
- 1.1k
- PR merge metrics
- No merged PRs in 30d
Description
Hi,
I am using a sparse index on the field fieldFoo and to best utilize it, I need to add an extra $exists condition before the $eqone.
When I manually create it via a criteria, the object is fine and working:
Document{{fieldFoo=Document{{$exists=true, $eq='123'}}}}
Now trying to achieve it via a repository method:
getByFieldFooExistsAndFieldFoo(String value); // not working - it expects a true/false for the exists part
or
getByFieldFooExistsIsTrueAndFieldFoo(String value); // also not working due to syntax
this workaround syntax appears fine:
getByFieldFooExistsAndFieldFoo(boolean exists, String value); // passing always true
but the current spring-data source code is not supporting it:
org.springframework.data.mongodb.InvalidMongoDbApiUsageException:
Due to limitations of the org.bson.Document, you can't add a second 'fieldFoo'
expression specified as 'fieldFoo : 123';
Criteria already contains 'fieldFoo : Document{{$exists=true}}'
It appears to be a combination of the MongoQueryCreator and the AbstractQueryCreator, where combinations are not supported.
Instead of simple a and chain of single conditions:
...
criteria.and("fieldFoo").exists(true)
criteria.and("fieldFoo").is("123")
...
we would need a capability of multi-conditions:
...
criteria.and("fieldFoo").exists(true).is("123")
...
Was this not added on purpose, or just not perceived as widely used/needed?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading MongoQueryCreator and AbstractQueryCreator to trace how the derived method getByFieldFooExistsAndFieldFoo is turned into Criteria. Compare the current single-condition chaining with the requested repeated-field conditions, then add coverage for the repository method and verify that both $exists and $eq are retained without a duplicate-field exception.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, mongodb, spring
- Domain
- backend, databases
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100