DEV Community

Jeremy Persing
Jeremy Persing

Posted on

MongoDB Equivalent of SELECT column FROM table

I've gotten lost in my notes too many times so this is a reference for my future self and anyone else who needs this.

In an aggregation pipeline, add the following.

$group {
    _id: null (or whatever),
    countries: {
        $addToSet: "$subCollection.countries"
    }
}
Enter fullscreen mode Exit fullscreen mode

This will result in

{
  _id: null,
  countries: ["a", "b", "c", etc...]
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)