public function sortByEntityName($direction)
    {
        if ($this->id != '$entity') {
            throw new AnalyticsDbException('In order to sort by entity name, you need to first group by entity name.');
        }

        $direction = (int)$direction;
        $this->pipeline['$sort'] = ['_id' => $direction];

        return $this;
    }