Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

837
Vistas
Spring data mongodb: Optional @Query parameter no longer works

After upgrading to spring data mongodb 1.10.1, I am getting errors when running queries like:

@Query("{$and :["
            + "{ $or : [ { $where: '?0 == null' } , { 'field1' : ?0 } ] },"
            + "{ $or : [ { $where: '?1 == null' } , { 'field2' : ?1 } ] },"
            + "]}")
public Page<Entity> findAll(String param1, String param2)

Checking the error I see the parameter inside the where clause is not quoted and as I result I get:

org.springframework.data.mongodb.UncategorizedMongoDbException: Query failed with error code 139 and error message 'ReferenceError: test_param_value is not defined :

I have seen a few answers here recommending this way of handling optional parameters ((spring-data-mongo - optional query parameters?)) but it no longer works and I cannot seem to find anything in the release change log.

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

In case anyone else is interested, I managed to find a workaround after checking a similar ticket int the Spring Data project.

It seems the way I was checking for null parameters in the query is not a good practice. This is from a Spring developer comment: "placeholders are not designed to compose keys/values but bind parameters. Beyond that, placeholder use in quoted strings is always problematic in terms of escaping. Using SpEL should suit your needs"

So I ended up using SpEL to do the checks on parameters and it works fine. This is how it looks:

@Query("{$and :[" 
        + "?#{ [0] == null ? { $where : 'true'} : { 'field1' : [0] } },"
        + "?#{ [1] == null ? { $where : 'true'} : { 'field2' : [1] } },"
        + "]}")
public Page<Entity> findAll(String param1, String param2, Pageable pageable);
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda