Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.