Skip to content

Instantly share code, notes, and snippets.

@jeffyuhaoliu
Created September 13, 2013 07:27
Show Gist options
  • Save jeffyuhaoliu/6547659 to your computer and use it in GitHub Desktop.
Save jeffyuhaoliu/6547659 to your computer and use it in GitHub Desktop.
Node.js MongoDB HW#5.1 - Use aggregation framework to calculate the author with the greatest number of comments. Note: To run this in Terminal, type in... mongo < [file name of this .js file]. This should run the command of the .js file you created with the mongo shell and return back results in Terminal.
use blog
db.posts.aggregate([{
$unwind: "$comments"
}, {
$group: {
_id: "$comments.author",
postcount: {
$sum: 1
}
}
}, {
$sort: {
postcount: 1
}
}])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment