Skip to content

Instantly share code, notes, and snippets.

@anapaulagomes
Last active December 19, 2016 16:59
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 anapaulagomes/138958fceb012c9616cb to your computer and use it in GitHub Desktop.
Save anapaulagomes/138958fceb012c9616cb to your computer and use it in GitHub Desktop.
Group array attributes, sum then and order from the biggest to the smallest - MongoDB
var pipeline = [{$unwind: '$comments'}, {$group: {_id: '$comments.author', numberOf: {$sum: 1}}}, {$sort: {numberOf:-1}}]
db.posts.aggregate(pipeline)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment