Skip to content

Criteria.getCriteriaObject invalid when using Criteria.expr #4685

Closed
@lecousin

Description

@lecousin

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]}}}}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions