3
votes

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!

1

1 Answers

2
votes

This is literally the first MongoDB query I ever wrote. It works, but happy to hear about improvements:

db.collection.aggregate([
    {
        $group: {
            _id: null,
            things: { $push: "$things"}
        }
    },
    {
        $project: {
            _id: 0,
            things: {
                $reduce: {
                    input: "$things",
                    initialValue: [],
                    in: {
                        $setUnion: [ "$$this", "$$value"]
                    }
                }
            }
        }
    },
    {
      $project: {
         item: 1,
         numberOfUniqueThings: {  $size: "$things" }
      }
   }
])