I have multiple documents in this form, where the Array is an array of objects whose contents don’t really matter

```
{ animals : { cats: Array[3], dogs: Array[3] }}
{ animals : { cats: Array[2], fish: Array[1] }}
{ animals : { dogs: Array[2] }}
```

What I’d like to do is compute things like the total, min, max and average count of each category and end up with something like this

```
{ cats { min: XXX, max: XXX, avg: XXX }}
{ dogs { min: XXX, max: XXX; avg: XXX }}
{ fish { min: XXX, max: XXX; avg: XXX }}
```

This pipeline comes close

```
[{
$project: {
foo: {
$objectToArray: '$animals'
}
}
}, {
$unwind: {
path: '$foo',
preserveNullAndEmptyArrays: true
}
}, {
$group: {
_id: '$foo.k',
min: {
$min: {
$size: '$foo.v'
}
},
max: {
$max: {
$size: '$foo.v'
}
},
avg: {
$avg: {
$size: '$foo.v'
}
},
sum: {
$sum: {
$size: '$foo.v'
}
}
}
}]
```

which returns

```
{ "_id": "cats", "min": 2, "max": 3, "avg": 2.5, "sum": 5 }
{ "_id": "dogs", "min": 2, "max": 3, "avg": 2.5, "sum": 5 }
{ "_id": "fish", "min": 1, "max": 1, "avg": 1, "sum": 1 }
```

which is close but it doesn’t account for missing items. I need to treat those as 0 values and incorporate them into the various calculations. The correct result would be

```
{ "_id": "cats", "min": 0, "max": 3, "avg": 1.666, "sum": 5 }
{ "_id": "dogs", "min": 0, "max": 3, "avg": 1.666, "sum": 5 }
{ "_id": "fish", "min": 0, "max": 1, "avg": 0.3333, "sum": 1 }
```