'Cannot use Comparison Operator eq in Mongo Aggregation Spring because of Spel error Expected 'rsquare(])' but was 'comma(,)'
I'm trying to replicate this stage in Java
{
evaluaciones: {
$map: {
input: '$evaluaciones',
as: 'eval',
in: {
porcentaje: '$$eval.porcentaje',
detalles: {
$map: {
input: '$$eval.detalles',
as: 'det',
in: {
porcentaje: '$$det.porcentaje',
productoAcademico: {
$first: {
$filter: {
input: '$prodAcads',
as: 'prod',
cond: {
$eq: ['$$prod._id', '$$det.productoAcademicoId']
}
}
}
}
}
}
}
}
}
}
}
By doing this
BsonDocument t = new BsonDocument().append("$map", new BsonDocument()
.append("input", new BsonString("$evaluaciones"))
.append("as", new BsonString("eval"))
.append("in", new BsonDocument()
.append("porcentaje", new BsonString("$$eval.porcentaje"))
.append("detalles", new BsonDocument()
.append("$map", new BsonDocument()
.append("input", new BsonString("$$eval.detalles"))
.append("as", new BsonString("det"))
.append("in", new BsonDocument()
.append("porcentaje", new BsonString("$$det.porcentaje"))
.append("productoAcademico", new BsonDocument()
.append("$first", new BsonDocument()
.append("$filter", new BsonDocument()
.append("input", new BsonString("$prodAcads"))
.append("cond", new BsonDocument()
.append("$eq", new BsonArray(
List.of(new BsonString("$$prod._id"),
new BsonString("$$det.productoAcademicoId"))
)
)
)
)
)
)
)
)
)
)
);
I also have used Aggregation API
VariableOperators.Map map = VariableOperators.Map
.itemsOf("$evaluaciones")
.as("eval")
.andApply(eval -> eval.getMappedObject(new Document()
.append("porcentaje", "$$eval.porcentajes")
.append("detalles", VariableOperators.Map
.itemsOf("$$eval.detalles")
.as("det")
.andApply(det -> det.getMappedObject(new Document()
.append("porcentaje", "$$det.porcentaje")
.append("productoAcademico", new Document().append("$first",
ArrayOperators
.arrayOf("$prodAcads")
.filter()
.as("prod")
.by(prod -> ComparisonOperators.Eq.
valueOf("$$prod._id")
.equalToValue("$$det.productoAcademicoId")
.toDocument()
)
)
)
)
)
)
)
);
I use as a part of an "$addField" here
AddFieldsOperation formatResults = Aggregation
.addFields()
.addField("evaluaciones").withValueOfExpression(t.toJson()) // Applied here
.addField("sistemaCalificacionId").withValue("$$REMOVE")
.addField("sistemaCalificacion").withValueOfExpression("{$first: '$sistemaCalificacion'}")
.build();
TypedAggregation<Curso> cursoDatoTypedAggregation = Aggregation.newAggregation(
Curso.class,
match,
sistemaCalificacion,
prodAcads,
formatResults // Here
);
return this.reactiveMongoTemplate
.aggregate(cursoDatoTypedAggregation, CursoDato.class)
.singleOrEmpty();
But in both cases I get this error
org.springframework.expression.spel.SpelParseException: Expression [{"$map": {"input": "$evaluaciones", "as": "eval", "in": {"porcentaje": "$$eval.porcentaje", "detalles": {"$map": {"input": "$$eval.detalles", "as": "det", "in": {"porcentaje": "$$det.porcentaje", "productoAcademico": {"$first": {"$filter": {"input": "$prodAcads", "cond": {"$eq": ["$$prod._id", "$$det.productoAcademicoId"]}}}}}}}}}}] @293: EL1043E: Unexpected token. Expected 'rsquare(])' but was 'comma(,)'
It seems that Spel doesn't accept Arrays. I tried to parse it as an object according to this but this didn't work. Is there any other way to replicate that stage?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
