Search code examples
mongodbspring-bootaggregation-frameworkspring-data-mongodbmongodb-aggregation

Springboot with Mongodb Criteria.orOperation doesn't work


I am trying to create one OR operation with Criteria in MongoDB with Spring-Boot, this is my code:

TypedAggregation<Ficha> agg = newAggregation(Ficha.class,    
   // match(Criteria.where("parte.nome").regex(".*"+ query+".*")),
   // match(Criteria.where("parte.nome").regex(".*"+ query+".*").orOperator(Criteria.where("parte.documentos.numero").regex(".*"+ query+".*"))),
   // match(Criteria.where("parte.documentos.numero").regex(".*"+query+".*")),
   group("parte.nome").first("parte.nome").as("nome"),
   project().and("nome").previousOperation().and("nome").as("nome")

If I uncomment line 1, it works (search only in parte.nome).

If I uncomment the last line, it works too (but searches only in parte.documentos.numero).

If I uncomment the middle line, it doesn`t work. How do I solve this?


Solution

  • Implement it as:

    TypedAggregation<Ficha> agg = newAggregation(Ficha.class,    
       match(new Criteria().orOperator(
            Criteria.where("parte.nome").regex(".*"+ query+".*"),
            Criteria.where("parte.documentos.numero").regex(".*"+ query+".*")
        )),  
       group("parte.nome").first("parte.nome").as("nome"),
       project().and("nome").previousOperation().and("nome").as("nome")
    );