Skip to content

Criteria.getCriteriaObject invalid when using Criteria.expr #4685

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

Closed
lecousin opened this issue Apr 6, 2024 · 4 comments
Closed

Criteria.getCriteriaObject invalid when using Criteria.expr #4685

lecousin opened this issue Apr 6, 2024 · 4 comments
Labels
status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged

Comments

@lecousin
Copy link

lecousin commented Apr 6, 2024

Following #2750 which adds the method Criteria.expr, when using the method Criteria.getCriteriaObject it generates an invalid Document because MongoExpression is not a Document.

I think that the method Criteria.getSingleCriteriaObject should call MongoExpression.toDocument() when a value is an instance of MongoExpression.

spring-data-mongodb version: 4.2.4

Small example:

Criteria criteria = Criteria.expr(ComparisonOperators.valueOf("field1").equalTo("field2"));
Document filter = criteria.getCriteriaObject();
System.out.println("Actual result:");
System.out.println(filter);
for (var entry : filter.entrySet()) {
	if (entry.getValue() instanceof MongoExpression e)
		entry.setValue(e.toDocument());
}
System.out.println("Expected result:");
System.out.println(filter);

output:

Actual result:
Document{{$expr=org.springframework.data.mongodb.core.aggregation.ComparisonOperators$Eq@31b7dea0}}
Expected result:
Document{{$expr=Document{{$eq=[$field1, $field2]}}}}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 6, 2024
@christophstrobl
Copy link
Member

The QueryMapper is in charge of converting non MongoDB default types taking potential customisations such as the @Field into account.

@mp911de
Copy link
Member

mp911de commented Apr 8, 2024

Zooming out: What are you trying to achieve?

@mp911de mp911de added the status: waiting-for-feedback We need additional information before we can continue label Apr 8, 2024
@lecousin
Copy link
Author

lecousin commented Apr 8, 2024

What are you trying to achieve?

I'm using ReactiveMongoTemplate to do custom queries without mapping to a Class, using mongoTemplate.getCollection(...) then methods such as MongoCollection.find.
For this I'm using the Criteria class to build the filter Bson, using the method getCriteriaObject, which works fine except for $expr.

I see there is also the AggregationExpressionCriteria implementing the CriteriaDefinition interface, which looks like doing the same as Criteria.expr, but it seems it is not used anywhere.

I can understand if you consider that the Criteria class is not supposed to be used to generate a valid Bson document that can be directly be sent to mongo, and that it must be used together with the QueryMapper (meaning with mapping to a given MongoPersistentEntity). For my use case I have an easy workaround which consists in going through the generated Document and call again toDocument on non-bson values.

However it would be nice that all implementations of CriteriaDefinition.getCriteriaObject generate a valid Document. May be the class AggregationExpressionCriteria can be used, and the Criteria class can accept to add any inner CriteriaDefinition ?

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Apr 8, 2024
@mp911de
Copy link
Member

mp911de commented Apr 8, 2024

The bigger issue is that Spring Data implementations need to express certain query operations and have to carry these around until they are transformed into a format that the underlying store understands. For better or worse, Document is essentially a Map accepting all sorts of values. We use the Query Document as value object for our own expressions that hold on to contextual values providing e.g. details on which type to map a query or what aggregation options to apply for aggregation stages.

CriteriaDefinition is not attached to any persistence meta-model information hence we cannot create a Document that you could use without further transformation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

No branches or pull requests

4 participants