title: How to combine multiple criteria in OR query dynamically in Play Morphia
body: <p>I am trying to use a kind of builder pattern to build an OR query using multiple criteria depending upon the scenario. An example is</p>
  
  <pre><code>    public class Stylist extends Model {
  
        public String firstName;
  
        public String lastName;
  
        public String status;
  
        ...
  
        }
  </code></pre>
  
  <p>I would like to search Stylist collection if the first name or last name matches a given string and also status matches another string. I am writing the query as follows:</p>
  
  <pre><code>      MorphiaQuery query = Stylist.q();
        if (some condition) {
        query.or(query.criteria("status").equal("PendingApproval"), query.criteria("status").equal(EntityStatus.ACTIVE));    
        }
  
        if (some other condition as well) {
        query.or(query.criteria("firstName").containsIgnoreCase(name), query.criteria("lastName").containsIgnoreCase(name));
        }
  </code></pre>
  
  <p>When both the conditions are met, I see that query contains only the criteria related to firstName and lastName i.e. different OR criteria are not added/appended but overwritten. It's quite different from filter criteria where all the different filter conditions are appended and you can easily build queries containing multiple AND conditions. </p>
  
  <p>I can solve the problem by putting my conditions differently and building my queries differently but doesn't seem to be an elegant way. Am I doing something wrong ?</p>
  
  <p>I am using Play! Framework 1.2.4 and Play Morphia module version 1.2.5a</p>
  
  <p><strong>Update</strong></p>
  
  <p>To put it more clearly, I would like to AND multiple OR queries. Concretely, in the above mentioned scenario, I would like to </p>
  
  <p>I would like to search for Stylists where :</p>
  
  <pre><code>firstName or lastName contains supplied name AND
  
  status equals ACTIVE or PENDING_APPROVAL.
  </code></pre>
  
  <p>I have been able to construct the query directly on Mongo shell through :</p>
  
  <p>db.stylists.find({$and: [{$or : [{status: "PENDING_APPROVAL"}, {status : "ACTIVE"}]},{$or : [{firstName : { "$regex" : "test" , "$options" : "i"}}, {lastName : { "$regex" : "test" , "$options" : "i"}}]}] }).pretty();</p>
  
  <p>But have not able to achieve the same through Query API methods. Here is my attempt :</p>
  
  <pre><code>    Query&lt;Stylist&gt; query = MorphiaPlugin.ds().find(Stylist.class);
  
      CriteriaContainer or3 = query.or(query.criteria("firstName").containsIgnoreCase(name), query.criteria("lastName").containsIgnoreCase(name));
  
      CriteriaContainer or4 = query.or(query.criteria("status").equal("PENDING_APPROVAL"), query.criteria("status").equal("ACTIVE"));
  
      query.and(or3, or4);
  </code></pre>
  
  <p>query.toString() results in following output : { "$or" : [ { "status" : "PENDING_APPROVAL"} , { "status" : "ACTIVE"}]}</p>
  
  <p>Not sure, where am I missing ?</p>