title: Mongodb Aggregation on Json Array (JAVA)
body: <p>I have a typical web application in which I am trying to generate facets from the mongodb collection. This is currently being done using the aggregation framework using the Java driver (v2.10.1). The facets are generated correctly, except for the documents containing sub-arrays, for instance I have the following json documents:</p>
  
  <ol>
  <li><code>{name: polo, fueltypes:[benzin, lpg], color: black}</code></li>
  <li><code>{name: golf, fueltypes:[benzin, cng], color: blue}</code></li>
  <li><code>{name: a4, fueltypes:[diesel], color: blue}</code></li>
  </ol>
  
  <p>The returned result set is:</p>
  
  <p>name:</p>
  
  <pre><code>{_id: polo, count: 1}
  {_id: golf, count: 1}
  {_id: a4, count: 1}
  </code></pre>
  
  <p>color:</p>
  
  <pre><code>{_id: black, count: 1}
  {_id: blue, count: 2}
  </code></pre>
  
  <p>fueltypes:</p>
  
  <pre><code>{_id: [benzin,lpg,cng,diesel], count: 3}
  </code></pre>
  
  <p>The aggregated result of the fueltypes field contains all the array fields. </p>
  
  <p>However the desired result should be:</p>
  
  <p>fueltypes:</p>
  
  <pre><code>{_id: benzin, count: 2}
  {_id: lpg, count: 1}    
  {_id: diesel, count: 1}    
  {_id: cng, count: 1}    
  </code></pre>
  
  <p>and the corresponding java code:</p>
  
  <pre><code>String str = "name" ; //or fueltypes, color
  // create match
  BasicDBObject match = new BasicDBObject();
  match.put("$match", new BasicDBObject());
  
  // build the $projection operation
  DBObject fields = new BasicDBObject();
  
  // fields.put("count", 1);
  DBObject project = new BasicDBObject();
  
  // Now the $group operation
  DBObject groupFields = new BasicDBObject();
  
  DBObject unwindFields = new BasicDBObject();
  // build the $projection operation
  fields.put(str, 1);
  project.put("$project", fields);
  
  // Now the $group operation
  groupFields.put("_id", "$" + str);
  
  // performing sum and storing it in the count attribute
  groupFields.put("count", new BasicDBObject("$sum", 1));
  
  DBObject group = new BasicDBObject("$group", groupFields);
  AggregationOutput output = serviceCollection.aggregate(match, project, group);
  </code></pre>