Skip to content

Instantly share code, notes, and snippets.

@ivan-loh
Created May 5, 2015 18:23
Show Gist options
  • Save ivan-loh/1d3e6c61c9353c561471 to your computer and use it in GitHub Desktop.
Save ivan-loh/1d3e6c61c9353c561471 to your computer and use it in GitHub Desktop.
MongoDB Aggregation equivalent of SQL SELECT COUNT(DISTINCT FieldName) FROM ColletionName
db.CollectionNameHere.aggregate([
{ $group: { _id: "$FieldNameHere"} },
{ $group: { _id: 1, count: { $sum: 1 } } }
], { allowDiskUse: true })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment