Pregunta

I have a Spring Data MongoDB Repository that I want to query with a search criteria defined as an object.

Details:

I have a model class:

@Document
public class ModelClass {

    @Id
    private String id;

    private String field1;
    private String field2;
    ...
    private String field10;

    // getters and setters
}

I also have a MongoRepository to store instances of such class:

public interface Repo extends MongoRepository<ModelClass,String> {}

I would like to query the repository using instances of ModelClass as search criteria.Such instances may not have values in all fields (some fields may hold null). So, in the Repo interface, I need something like:

List<ModelClass> findByXXXX(ModelClass criteria);

The goal is to query the Repo without the need of writing all the possible combinations of fields.

¿Fue útil?

Solución

For very dynamic queries, Spring Data JPA integrates with Querydsl. It allows you to define predicates on the fly and execute them as MongoDB queries. The reference documentation has details on that.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top