'Work-in-progress function to get scan results grouped by
common open ports'

def group_by_port(self, flt, archive=False):

    aggr = []
    if flt:
        aggr += [{'$match': flt}]
    aggr += [{'$project': {'ports.port': 1, 'ports.state_state': 1}}, {'$project': {'ports': {'$ifNull': ['$ports', []]}}}, {'$redact': {'$cond': {'if': {'$eq': [{'$ifNull': ['$ports', None]}, None]}, 'then': {'$cond': {'if': {'$eq': ['$state_state', 'open']}, 'then': '$$KEEP', 'else': '$$PRUNE'}}, 'else': '$$DESCEND'}}}, {'$project': {'ports': {'$cond': [{'$eq': ['$ports', []]}, [0], '$ports.port']}}}, {'$group': {'_id': '$ports', 'ids': {'$addToSet': '$_id'}}}]
    return self.db[(self.colname_oldhosts if archive else self.colname_hosts)].aggregate(aggr, cursor={})
