Here is an extended solution that will get the distribution of the value of all elements of field.array.
db.foo.aggregate([
{$addFields: {distrib: {$reduce: {
input: "$field.array",
initialValue: {"1":0,"2":0,"3":0,"4":0,"5":0},
in: {
"1":{$add:["$$value.1",{$toInt:{$eq:[1,"$$this"]}}]},
"2":{$add:["$$value.2",{$toInt:{$eq:[2,"$$this"]}}]},
"3":{$add:["$$value.3",{$toInt:{$eq:[3,"$$this"]}}]},
"4":{$add:["$$value.4",{$toInt:{$eq:[4,"$$this"]}}]},
"5":{$add:["$$value.5",{$toInt:{$eq:[5,"$$this"]}}]}
}
}}
}}
]);
And typically the follow-on question is how to get distributions across multiple docs, which in a way is "easier" because $bucket works across a pipeline of documents:
db.foo.aggregate([
{$unwind: "$field.array"},
{$bucket: {
groupBy: "$field.array",
boundaries: [1,2,3,4,5,6,7,8,9,10],
output: {
n: {$sum:1}
}
}}
]);
Alternately, you can add this stage after the $addFields/$reduce stage. It yields messy arrays of 1 object of which only field n is interesting but you can easily get the value in the client-side with doc['1'][0]['n'], doc['2'][0]['n'], etc.
,{$facet: {
"1": [ {$group: {_id:null, n:{$sum:"$distrib.1"}}} ],
"2": [ {$group: {_id:null, n:{$sum:"$distrib.2"}}} ],
"3": [ {$group: {_id:null, n:{$sum:"$distrib.3"}}} ],
"4": [ {$group: {_id:null, n:{$sum:"$distrib.4"}}} ],
"5": [ {$group: {_id:null, n:{$sum:"$distrib.5"}}} ]
}}
If you really want to make the return structure simple, add this stage at the end to collapse the [0]['n'] data:
,{$addFields: {
"1": {$let:{vars:{q:{$arrayElemAt:["$1",0]}},in: "$$q.n"}},
"2": {$let:{vars:{q:{$arrayElemAt:["$2",0]}},in: "$$q.n"}},
"3": {$let:{vars:{q:{$arrayElemAt:["$3",0]}},in: "$$q.n"}},
"4": {$let:{vars:{q:{$arrayElemAt:["$4",0]}},in: "$$q.n"}},
"5": {$let:{vars:{q:{$arrayElemAt:["$5",0]}},in: "$$q.n"}}
}}
In MongoDB 5.0, the new $getField function makes this a little more straightforward:
,{$addFields: {
"1": {$getField:{input:{$arrayElemAt:["$1",0]}, field:"n"}}
...