Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ackuser/ab43242745081d29421800568689d83a to your computer and use it in GitHub Desktop.
Save ackuser/ab43242745081d29421800568689d83a to your computer and use it in GitHub Desktop.
MongoDB Aggregation Framework way for "select count (distinct fieldName) from someTable" for large collections
//equivalent of "select count (distinct fieldName) from someTable"
db.someCollection.aggregate([{ $group: { _id: "$fieldName"} },{ $group: { _id: 1, count: { $sum: 1 } } } ])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment