Skip to content

Instantly share code, notes, and snippets.

@cjsaylor
Last active December 27, 2015 12:09
Show Gist options
  • Save cjsaylor/7323403 to your computer and use it in GitHub Desktop.
Save cjsaylor/7323403 to your computer and use it in GitHub Desktop.
Category aggregate query
var getTopCategory = function(category, callback) {
var sorter = {};
sorter[category] = -1;
this.collection('results').aggregate([
{ $sort: sorter },
{ $project: {
_id: 0,
name: 1,
category_value: '$' + category
}},
{ $limit: 1 },
], function(err, result) {
callback(err, result);
});
}
var topCategoryList = function(db, callback) {
var map = ['kills', 'damage', 'round', 'waveClears'],
list = {};
if (!db) {
callback('DB not available.');
return;
}
Async.map(map, _.bind(getTopCategory, db), function(err, results) {
if (err) {
callback(err);
return;
}
_.each(results, function(result, index) {
list[map[index]] = result[0];
})
callback(null, list);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment