Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support derived queries using the same field multiple times #4715

Open
hadjiski opened this issue Jun 5, 2024 · 1 comment
Open

Support derived queries using the same field multiple times #4715

hadjiski opened this issue Jun 5, 2024 · 1 comment
Labels
type: enhancement A general enhancement

Comments

@hadjiski
Copy link

hadjiski commented Jun 5, 2024

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?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 5, 2024
@mp911de
Copy link
Member

mp911de commented Jun 10, 2024

We do not support the implicit $and form using our Criteria API, however, we should explore how to enable querying the same field multiple times through our query derivation mechanism.

In the meantime, please use String-based @Query where you supply the desired query string yourself.

@mp911de mp911de added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 10, 2024
@mp911de mp911de changed the title Spring data mongodb repository name convention does not support combination including Exists for a field Support derived queries using the same field multiple times Jun 10, 2024
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

No branches or pull requests

3 participants