title: Java query for mongodb to find documents where list size is less than n
body: <p>I'm trying to find all of the documents in my db where the the size of my "states" list only contains one state but I'm struggling with the syntax of the java code. </p>
  
  <p>
  My db looks like this:</p>
  
  <pre><code>{ "_id" : 13218 , "country" : { "MY" : 11 , "US" : 4} , "state" : { "WA" : 4 }}
  { "_id" : 95529 , "country" : { "US" : 6 } , "state" : { "MI" : 6 }}
  { "_id" : 22897 , "country" : { "US" : 4 } , "state" : { "CA" : 2 , "TX" : 1 , "WY" : 1 }}
  </code></pre>
  
  <p>What I want to do is print out every "_id" found from the US that only has a single state. So, the only "_id" that'd be returned here is 95529.</p>
  
  <p>here is the relevant portion of code: </p>
  
  <pre><code>DBObject query = new BasicDBObject("country.US", new BasicDBObject("$gt", 4));
  //query.put("state.2", new BasicDBObject("$exists", true)); 
  //This is my attempt at checking the list length but it doesn't work
  DBCursor dbCursor = dBcollection.find(query);
  while (dbCursor.hasNext()){
      DBObject record = dbCursor.next();
      Object _id= record.get("_id");
      Object state= record.get("state");      
      System.out.println(_id + "," + state);
  }
  </code></pre>
  
  <p>current output looks like this:</p>
  
  <pre><code>95529, { "MI" : 6 }
  22897, { "CA" : 2 , "TX" : 1 , "WY" : 1 }
  </code></pre>