3

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!

0

1 Answer 1

2

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" }
      }
   }
])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.