code: private BasicDBObject createAggregation(EntityMetadata metadata)
      {
          if (kunderaQuery.getSelectStatement() != null)
          {
              Metamodel metaModel = kunderaMetadata.getApplicationMetadata().getMetamodel(metadata.getPersistenceUnit());
              EntityType entityType = metaModel.entity(metadata.getEntityClazz());
              AbstractManagedType managedType = (AbstractManagedType) metaModel.entity(metadata.getEntityClazz());
              boolean hasLob = managedType.hasLobAttribute();
  
              BasicDBObject aggregation = new BasicDBObject();
  
              SelectClause selectClause = (SelectClause) kunderaQuery.getSelectStatement().getSelectClause();
              Expression expression = selectClause.getSelectExpression();
  
              buildAggregation(aggregation, expression, metadata, entityType, hasLob);
  
              if (aggregation.size() == 0)
              {
                  return null;
              }
  
              if (!aggregation.containsField("_id"))
              {
                  aggregation.put("_id", null);
              }
  
              return aggregation;
          }
  
          return null;
      }
docs: Get the aggregation object.
  
  @param metadata
  @return