Skip to content

Instantly share code, notes, and snippets.

@banker
Created August 31, 2010 15:58
Show Gist options
  • Save banker/559251 to your computer and use it in GitHub Desktop.
Save banker/559251 to your computer and use it in GitHub Desktop.
MongoDB Aggregation
Min and Max
db.posts.find({}).sort({x: 1}).limit(1) return the post with the smallest value for x
db.posts.find({}).sort({x: -1}).limit(1) return the post with the largest value for x
Note: min and max queries are most efficient with an index on the field being sorted on.
Count
db.posts.count(selector) count the number of posts matching the given selector
db.posts.count({}) count the total number of posts
db.posts.count({"comment_size": {"$gt": 5}}) get the number of posts with more than five comments
Distinct
db.posts.distinct(field_name, filter) get a distinct list of values for the given _field_name_. Include an optional filter.
db.posts.distinct('comments.username') return a list of distinct users who have commented on these posts
db.posts.distinct('tags', {"comment_size": {"$gt": 5}}) get a distinct list of tags for posts with more than five comments
Group
Group works on non-sharded MongoDB deployments only, but is currently the fastest way to build custom aggregations.
Three parameters are required: key, initial, and reduce. key is the field to group on, and initial is the first value of prev sent to the reduce function.
This code groups comments by username and counts up the totals:
db.comments.group({key: {username: true}, initial: {sum: 0}, reduce: "function(doc, prev) {prev.sum += 1;}"});
Map/Reduce
MongoDB's Map-Reduce allows you to write custom aggregations in JavaScript. The results are always stored in a new collection.
Here we specify the necessary map and reduce functions to output unique tags and their counts to a collection called 'results.' The
reduce function must return a document with the same form as what's emitted in the map function.
map = "function() { this.tags.forEach(function(tag) { emit(tag, {count: 1}); }); }";
reduce = "function(key, value_array) { var sum = 0; value_array.forEach(function(doc) { sum += doc.count; }); return {count: sum} }";
db.posts.mapReduce(map, reduce, {out: 'results'});
Common Map/Reduce options
query : <query filter object>
out : <output-collection name> a string specifying the name of the output collection
finalize : <finalize function> a JavaScript function for munging the final result set
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment