title: Spring Mongo criteria querying twice the same field
body: <p>I am trying to query mongodb using spring. We have a collection which holds a tree and a include a list of items as a tree path (so we can easily traverse the tree). We have a query which needs to return all child nodes of a specific node. Our query is based on selecting all nodes that have the node (parent) in the path and are one level lower than the parent (level). Our criteria is as follows:</p>
  
  <pre><code>Criteria.where("treePath").in(parentId).and("treePath").size(level)
  </code></pre>
  
  <p>Alas, when we call this on mongodb we get the following exception:</p>
  
  <blockquote>
    <p>org.springframework.data.mongodb.InvalidMongoDbApiUsageException: Due
    to limitations of the com.mongodb.BasicDBObject, you can't add a
    second 'treePath' expression specified as 'treePath : { "$size" : 2}'.
    Criteria already contains 'treePath : { "$in" : [
    "50137df5f49f9b4a6481d639"]}'.</p>
  </blockquote>
  
  <p>Are there other suggestions on how to achieve the same? One option I was thinking of was to query mongodb directly. I tried</p>
  
  <pre><code>String command = "{findAndModify:\"Task\",query:{$and:[{treePath:\"5013a79a36600872ecf4dba8\"},{treePath:{$size:2}},{order:{$gte:0}}]},update:{$inc:{order:1}}}";
  CommandResult commandResult = mongoTemplate.executeCommand(command);
  </code></pre>
  
  <p>But this will only update the first record and I need them all updated.</p>
  
  <p>Thanks!</p>