Skip to content

Instantly share code, notes, and snippets.

@kyriesent
Last active December 13, 2015 17:38
Show Gist options
  • Save kyriesent/4948970 to your computer and use it in GitHub Desktop.
Save kyriesent/4948970 to your computer and use it in GitHub Desktop.
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)
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... ]
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
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