code: private void buildAggregation(DBObject group, Expression expression, EntityMetadata metadata, EntityType entityType,
              boolean hasLob)
      {
          if (expression instanceof AggregateFunction)
          {
              AggregateFunction aggregateFunction = (AggregateFunction) expression;
              String identifier = aggregateFunction.getIdentifier().toLowerCase();
  
              Expression child = aggregateFunction.getExpression();
  
              if (child instanceof StateFieldPathExpression)
              {
                  StateFieldPathExpression sfpExp = (StateFieldPathExpression) child;
                  String columnName = getColumnName(metadata, entityType, sfpExp.toActualText());
                  String actualColumnName = columnName;
                  if (hasLob)
                  {
                      actualColumnName = "metadata." + columnName;
                  }
                  else if (metadata.getIdAttribute().equals(entityType.getAttribute(metadata.getFieldName(columnName))))
                  {
                      actualColumnName = "_id";
                  }
  
                  BasicDBObject item = new BasicDBObject("$" + identifier, "$" + actualColumnName);
                  group.put(identifier + "_" + columnName, item);
              }
              else if (expression instanceof CountFunction)
              {
                  group.put("count", new BasicDBObject("$sum", 1));
              }
          }
          else if (expression instanceof CollectionExpression)
          {
              for (Expression child : expression.children())
              {
                  buildAggregation(group, child, metadata, entityType, hasLob);
              }
          }
          else if (expression instanceof StateFieldPathExpression)
          {
              StateFieldPathExpression sfpExp = (StateFieldPathExpression) expression;
  
              BasicDBObject idObject;
              Object existing = group.get("_id");
              if (existing != null)
              {
                  idObject = (BasicDBObject) existing;
              }
              else
              {
                  idObject = new BasicDBObject();
                  group.put("_id", idObject);
              }
  
              String columnName = getColumnName(metadata, entityType, sfpExp.toActualText());
              String actualColumnName = columnName;
              if (hasLob)
              {
                  actualColumnName = "metadata." + columnName;
              }
  
              idObject.put(columnName, "$" + actualColumnName);
          }
      }
docs: Build the aggregation parameters.
  
  @param group
  @param expression
  @param metadata
  @param entityType