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