Skip to content

Instantly share code, notes, and snippets.

@gurdotan
Created February 9, 2014 13:59
Show Gist options
  • Save gurdotan/8899480 to your computer and use it in GitHub Desktop.
Save gurdotan/8899480 to your computer and use it in GitHub Desktop.
MongoDB corollary of SQL's "SELECT DISTINCT ... GROUP BY"
db.events.aggregate(
{'$group' : {_id : "$platform", devices: {"$addToSet" : "$device_id"} } },
{"$unwind" : "$devices" },
{"$group" : {_id : "$_id", count: {"$sum" : 1}}}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment