Skip to content

Instantly share code, notes, and snippets.

@xjamundx
Created April 20, 2011 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xjamundx/931616 to your computer and use it in GitHub Desktop.
Save xjamundx/931616 to your computer and use it in GitHub Desktop.
mongo group tag cloud stuff
app.get('/find/other', function(req, res) {
var reduce = function(obj, prev) {
obj.tags.forEach(function(tag) {
prev.tagCounts[tag] = prev.tagCounts[tag] ? prev.tagCounts[tag] + 1 : 1;
})
};
db.reviews.group({}, {}, {tagCounts:{}}, reduce, function(err, result) {
// get the tags
var tags = result[0].tagCounts,
sorted = [],
scale = 0;
// sort the tags
for (tag in tags) {
sorted.push({
tag: tag,
count:tags[tag],
});
}
// sort the tags
sorted.sort(function(a, b) {
return a.count > b.count;
});
// figure out the scale and apply it
scale = sorted[sorted.length - 1].count;
sorted.forEach(function(tag) {
tag.num = (tag.count / scale).toFixed(1);
});
// randomize
sorted.sort(function() {
return 0.5 - Math.random()
})
// render tags page
res.render('find/tags.html', {
tags: sorted
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment