def count_distinct(collection, key, exclude=None):
    """Get the number of distinct values for key in the specified database collection"""

    values = collection.distinct(key)
    if exclude is not None and exclude in values:
        values.remove(exclude)
    return len(values)