Since this is the only part of the dict that varies I would split this along only the varying part, replacing line 139 to 152 with something like below, where we pull out a helper function:

```python
def _get_id_for_type(record, mongo_field):
    date_field = datetime_from_str(record[mongo_field])
    mongo_str = '$%s' % mongo_field
    return {"$substr": [mongo_str, 0, 10]} if isinstance(date_field, datetime) else mongo_str

group = {"count": {"$sum": 1}}
group["_id"] = _get_id_for_type(record, mongo_field)
```