Last active
December 27, 2015 12:09
-
-
Save cjsaylor/7323403 to your computer and use it in GitHub Desktop.
Category aggregate query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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