public
Last active

MongoDB Aggregation Framework way for "select count (distinct fieldName) from someTable" for large collections

  • Download Gist
gistfile1.js
JavaScript
1 2
//equivalent of "select count (distinct fieldName) from someTable"
db.someCollection.aggregate([{ $group: { _id: "$fieldName"} },{ $group: { _id: 1, count: { $sum: 1 } } } ])

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.