Skip to content

Instantly share code, notes, and snippets.

@kyriesent
Last active December 13, 2015 17:38

Revisions

  1. kyriesent revised this gist Feb 14, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions tagaggregate.coffee
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    Works.aggregate [
    $unwind: 'tags'
    $unwind: '$tagsL'
    ,
    $group: _id: 'tags', c: {$sum: 1}
    $group: _id: '$tagsL', c: {$sum: 1}
    ], (err, tags) ->
    return done err if err
    console.log tags # [ { _id: 'javascript', c:40 }, etc... ]
  2. kyriesent revised this gist Feb 14, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion tagaggregate.coffee
    Original file line number Diff line number Diff line change
    @@ -4,4 +4,4 @@ Works.aggregate [
    $group: _id: 'tags', c: {$sum: 1}
    ], (err, tags) ->
    return done err if err
    console.log tags # [ { _id: 'javascript', c:1 }, etc... ]
    console.log tags # [ { _id: 'javascript', c:40 }, etc... ]
  3. kyriesent revised this gist Feb 14, 2013. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions tagaggregate.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    Works.aggregate [
    $unwind: 'tags'
    ,
    $group: _id: 'tags', c: {$sum: 1}
    ], (err, tags) ->
    return done err if err
    console.log tags # [ { _id: 'javascript', c:1 }, etc... ]
  4. kyriesent revised this gist Feb 14, 2013. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions nativemapreduce.js
    Original file line number Diff line number Diff line change
    @@ -2,10 +2,12 @@ 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)
  5. kyriesent revised this gist Feb 14, 2013. 1 changed file with 20 additions and 0 deletions.
    20 changes: 20 additions & 0 deletions tagmapreducewrong.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    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
  6. kyriesent revised this gist Feb 14, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion tagmapreduce.coffee
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ o.map = ->
    else
    return
    o.reduce = (k, vals) ->
    vals.length
    Array.sum vals
    o.out = inline: 1
    o.verbose = true
    mongoose.models.Work.mapReduce o, (err, tagsArray, stats) ->
  7. kyriesent revised this gist Feb 14, 2013. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions nativemapreduce.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    map = function() {
    return this.tagsL.forEach(function(tagL, i) {
    return emit(tagL, 1);
    });
    }

    reduce = function(k, vals) {
    print (k)
    print (vals)
    return vals.length;
    };
  8. kyriesent revised this gist Feb 14, 2013. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions tagmapreduce.coffee
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,10 @@
    o = {}
    o.map = ->
    @tagsL.forEach (tagL, i) ->
    emit tagL, 1
    if @tagsL
    @tagsL.forEach (tagL, i) ->
    emit tagL, 1
    else
    return
    o.reduce = (k, vals) ->
    vals.length
    o.out = inline: 1
  9. kyriesent created this gist Feb 13, 2013.
    17 changes: 17 additions & 0 deletions tagmapreduce.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    o = {}
    o.map = ->
    @tagsL.forEach (tagL, i) ->
    emit tagL, 1
    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