I've got simple documents like
{
...,
things: ["a", "b", "c"]
},
{
...,
things: ["b", "d", "e"]
},
...
and I want a total count of distinct things, in this case: ["a","b","c","d","e"]
, the result should be 5.
How to do this using an aggregate query?
My current attempt is
[
{
"$group": {
"_id": "things",
"things": { "$push": "$things" }
}
},
{
"$addFields": {
"allThings": {
"$reduce": {
"input": "$things",
"initialValue": [],
"in": {
"$concatArrays": ["$$value", "$$this"]
}
}
}
}
}
]
which yields all fields in a single array, but I have no idea how to count distinct values in there now.
Happy for any help!