code: protected function createQueryWithFilterAndLastVersion(
          PaginateFinderConfiguration $configuration,
          $siteId,
          $language,
          $elementName,
          $order = array()
      ){
          $qa = $this->createAggregationQueryBuilderWithSiteIdAndLanguage($siteId, $language);
          $qa->match(array('deleted' => false));
          $filters = $this->getFilterSearch($configuration);
          if (!empty($filters)) {
              $qa->match($filters);
          }
  
          $qa->sort(array('createdAt' => 1));
  
          $group = array(
              '_id' => array('nodeId' => '$nodeId'),
              $elementName => array('$last' => '$$ROOT')
          );
          $groupOrder = array();
          foreach ($order as $name => $dir) {
              $nameOrder = str_replace('.', '_', $name);
              $groupOrder[$nameOrder] = $dir;
              $group[$nameOrder] = array('$last' => '$'.$name);
          }
          $qa->group($group);
  
          if (!empty($groupOrder)) {
              $qa->sort($groupOrder);
          }
  
          return $qa;
      }
docs: @param PaginateFinderConfiguration $configuration
  @param string                      $siteId
  @param string                      $language
  @param string                      $elementName
  @param array                       $order
  
  @return Stage