Skip to content

Instantly share code, notes, and snippets.

@sebastianvoss
Last active December 20, 2015 13:39

Revisions

  1. sebastianvoss renamed this gist Aug 2, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. sebastianvoss created this gist Aug 2, 2013.
    20 changes: 20 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    // map function
    m = function () {
    this.images.forEach( function(id) {
    emit(id, 1);
    })
    }
    // reduce function
    r = function (id, count) {
    return Array.sum(count);
    }
    // create a image count in collection map_reduce_sample
    db.albums.mapReduce(m, r, {out: "map_reduce_sample"})
    // tag all images which are in use
    db.map_reduce_sample.find().forEach(function(d) {
    db.images.update({_id: d._id}, {$set: {in_use: true}})
    })
    // remove unused images
    db.images.remove({in_use: {$exists: false}})
    // remove in_use tag
    db.images.update({}, {$unset: {in_use: ""}})