title: MongoDB & Spring Data - Searching for a user by firstname/lastname
body: <p>I have the following collection:</p>
  
  <pre><code>User {
     firstname,
     lastname
  }
  </code></pre>
  
  <p>I want to search for users by firstname and/or lastname (firstname/lastname/firstname+lastname).</p>
  
  <p>What I have:</p>
  
  <pre><code> Criteria criteria = new Criteria();
   criteria.orOperator(Criteria.where("firstname").regex(searchQuery),Criteria.where("lastname").regex(searchQuery));
  </code></pre>
  
  <p>The problem with the above is that I can either search by firstname OR lastname, but not both (e.g. ''John Smith'' is not possible while ''John'' or ''Smith'' is possible. 
  This is happening because of the orOperator and I can not see any andOr or something similar.</p>
  
  <p>So e.g. if I have "John Smith" inside my database, I can find him by querying either "John" , "Smith" , "John Smith" (just as in facebook for example)</p>
  
  <p>Edit:</p>
  
  <pre><code>String queryx[] = searchQuery.split(" ");
  for(String q : queryx) {
      criteria.orOperator(Criteria.where("firstname").regex(searchQuery),Criteria.where("lastname").regex(q));
  }
  </code></pre>
  
  <p>Shows me the results, but it gives the following error inside my console:</p>
  
  <pre><code>  Due to limitations of the com.mongodb.BasicDBObject, you can't add a second '$or' expression specified as '$or : [ { "firstname" : { "$regex" : "john smith"}} , { "lastname" : { "$regex" : "smith"}}]'. Criteria already contains '$or : [ { "firstname" : { "$regex" : "john smith"}} , { "lastname" : { "$regex" : "john"}}]'.
  </code></pre>
  
  <p>Edit2:</p>
  
  <pre><code>String queryx[] = searchQuery.split(" ");
     ArrayList&lt;Criteria&gt; orOperators = new ArrayList&lt;Criteria&gt;();
  for(String q : queryx) {
      Criteria criteria = new Criteria();
      criteria.orOperator(Criteria.where("firstname").regex(searchQuery),Criteria.where("lastname").regex(q));
      orOperators.add(criteria);
  }
  
  Criteria [] orArray = orOperators.toArray(new Criteria[orOperators.size()]);
  Criteria c = Criteria.where("firstname").regex(searchQuery);
  c.orOperator(orArray);
  
  Query query = new Query(c).limit(limit);
  return mongoOperations.find(query, User.class);
  </code></pre>
  
  <p>Edit 3:</p>
  
  <pre><code>String queryx[] = searchQuery.split(" ");
  ArrayList&lt;Criteria&gt; cr = new ArrayList&lt;Criteria&gt;();
  for (String q : queryx) {
      Criteria criteria = new Criteria();
      criteria.orOperator(Criteria.where("firstname").regex(q),
              Criteria.where("lastname").regex(q));
      cr.add(criteria);
  }
  
  Query query = new Query();
  for (Criteria cri : cr) {
      query.addCriteria(cri); //this is the problem
  }
  
  return mongoOperations.find(query, User.class);
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code> Due to limitations of the com.mongodb.BasicDBObject, you can't add a second 'null' criteria. Query already contains '{ "$or" : [ { "firstname" : { "$regex" : "john smith"}} , { "lastname" : { "$regex" : "john"}}]}'.
  </code></pre>