'This method makes use of the aggregation framework to
produce top values for a given field.'

def _topvalues(self, field, flt=None, topnbr=10, sort=None, limit=None, skip=None, least=False, aggrflt=None, specialproj=None, specialflt=None, countfield=None):

    if (flt is None):
        flt = self.flt_empty
    if (aggrflt is None):
        aggrflt = self.flt_empty
    if (specialflt is None):
        specialflt = []
    pipeline = []
    if flt:
        pipeline += [{'$match': flt}]
    if ((sort is not None) and ((limit is not None) or (skip is not None))):
        pipeline += [{'$sort': OrderedDict(sort)}]
    if (skip is not None):
        pipeline += [{'$skip': skip}]
    if (limit is not None):
        pipeline += [{'$limit': limit}]
    project = ({'_id': 0, field: 1} if (specialproj is None) else specialproj)
    if (countfield is not None):
        project[countfield] = 1
    pipeline += [{'$project': project}]
    for i in range(field.count('.'), (-1), (-1)):
        subfield = field.rsplit('.', i)[0]
        if (subfield in self.needunwind):
            pipeline += [{'$unwind': ('$' + subfield)}]
    pipeline += specialflt
    project = {'field': ('$%s' % field)}
    if (countfield is not None):
        project['count'] = ('$%s' % countfield)
    pipeline += [{'$project': project}]
    if aggrflt:
        pipeline += [{'$match': aggrflt}]
    else:
        pipeline += [{'$match': {'field': {'$exists': True}}}]
    pipeline += [{'$group': {'_id': '$field', 'count': {'$sum': (1 if (countfield is None) else '$count')}}}]
    if least:
        pipeline += [{'$sort': {'count': 1}}]
    else:
        pipeline += [{'$sort': {'count': (-1)}}]
    if (topnbr is not None):
        pipeline += [{'$limit': topnbr}]
    return pipeline
