I have set up MongoDB following this tutorial
http://www.littlelostmanuals.com/2011/09/spring-mongodb-type-safe-queries.html
Everything works as expected but now I'm stuck at a point where I want to be able to query on multiple fields.
Currently I have repository interfaces for each type I'm saving and can search on a single field fine.
public interface StartedEventRepository extends
MongoRepository<DatablockStartedEvent, String>,
QueryDslPredicateExecutor<DatablockStartedEvent> {
}
Below is the query for a single parameter.
return startedEventRepo
.findOne(QDatablockStartedEvent.datablockStartedEvent.searchId
.eq(searchId));
Is it possible to create a Query object where I can say something similar to the following.
if(someName != null){
query.where(QMyClass.name.eq(someName));
}
if(someTime != null){
query.where(QMyClass.time.eq(someTime));
}
List result = query.list();
I've tried looking at the MongodbQuery but I couldn't get it to work. Any ideas?
I saw an example http://www.mkyong.com/mongodb/spring-data-mongodb-update-document/ but this uses the mongoTemplate. Is there no way to achieve this through the repositories and if not, are they useless?