Last active
December 13, 2015 17:38
-
-
Save kyriesent/4948970 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
map = function() { | |
return this.tagsL.forEach(function(tagL, i) { | |
return emit(tagL, 1); | |
}); | |
}; | |
reduce = function(k, vals) { | |
print (k) | |
print (vals) | |
return vals.length; | |
}; | |
db.works.mapreduce(map, reduce) |
This file contains hidden or 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
Works.aggregate [ | |
$unwind: '$tagsL' | |
, | |
$group: _id: '$tagsL', c: {$sum: 1} | |
], (err, tags) -> | |
return done err if err | |
console.log tags # [ { _id: 'javascript', c:40 }, etc... ] |
This file contains hidden or 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
o = {} | |
o.map = -> | |
if @tagsL | |
@tagsL.forEach (tagL, i) -> | |
emit tagL, 1 | |
else | |
return | |
o.reduce = (k, vals) -> | |
Array.sum vals | |
o.out = inline: 1 | |
o.verbose = true | |
mongoose.models.Work.mapReduce o, (err, tagsArray, stats) -> | |
return done err if err | |
console.log "Tag Count took %d ms", stats.processtime | |
tags = {} | |
async.forEach tagsArray, (tag, cb) -> | |
tags[tag._id] = tag.value | |
cb() | |
, (err) -> | |
tags #a key value list of tag: count |
This file contains hidden or 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
o = {} | |
o.map = -> | |
if @tagsL | |
@tagsL.forEach (tagL, i) -> | |
emit tagL, 1 | |
else | |
return | |
o.reduce = (k, vals) -> | |
vals.length | |
o.out = inline: 1 | |
o.verbose = true | |
mongoose.models.Work.mapReduce o, (err, tagsArray, stats) -> | |
return done err if err | |
console.log "Tag Count took %d ms", stats.processtime | |
tags = {} | |
async.forEach tagsArray, (tag, cb) -> | |
tags[tag._id] = tag.value | |
cb() | |
, (err) -> | |
tags #a key value list of tag: count |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment