arnaucube
arnaucube/thoughtsPublic Self-hosted repository
thoughts/node_modules/mongoose/examples/mapreduce/mapreduce.js
node_modules/mongoose/examples/mapreduce/mapreduce.jsRaw
  1// import async to make control flow simplier
  2var async = require('async');
  3
  4// import the rest of the normal stuff
  5var mongoose = require('../../lib');
  6
  7require('./person.js')();
  8
  9var Person = mongoose.model('Person');
 10
 11// define some dummy data
 12var data = [
 13  {
 14    name: 'bill',
 15    age: 25,
 16    birthday: new Date().setFullYear((new Date().getFullYear() - 25)),
 17    gender: 'Male'
 18  },
 19  {
 20    name: 'mary',
 21    age: 30,
 22    birthday: new Date().setFullYear((new Date().getFullYear() - 30)),
 23    gender: 'Female'
 24  },
 25  {
 26    name: 'bob',
 27    age: 21,
 28    birthday: new Date().setFullYear((new Date().getFullYear() - 21)),
 29    gender: 'Male'
 30  },
 31  {
 32    name: 'lilly',
 33    age: 26,
 34    birthday: new Date().setFullYear((new Date().getFullYear() - 26)),
 35    gender: 'Female'
 36  },
 37  {
 38    name: 'alucard',
 39    age: 1000,
 40    birthday: new Date().setFullYear((new Date().getFullYear() - 1000)),
 41    gender: 'Male'
 42  }
 43];
 44
 45
 46mongoose.connect('mongodb://localhost/persons', function(err) {
 47  if (err) throw err;
 48
 49  // create all of the dummy people
 50  async.each(data, function(item, cb) {
 51    Person.create(item, cb);
 52  }, function(err) {
 53    if (err) {
 54      // handle error
 55    }
 56
 57    // alright, simple map reduce example. We will find the total ages of each
 58    // gender
 59
 60    // create the options object
 61    var o = {};
 62
 63    o.map = function() {
 64      // in this function, 'this' refers to the current document being
 65      // processed. Return the (gender, age) tuple using
 66      /* global emit */
 67      emit(this.gender, this.age);
 68    };
 69
 70    // the reduce function receives the array of ages that are grouped by the
 71    // id, which in this case is the gender
 72    o.reduce = function(id, ages) {
 73      return Array.sum(ages);
 74    };
 75
 76    // other options that can be specified
 77
 78    // o.query = { age : { $lt : 1000 }}; // the query object
 79    // o.limit = 3; // max number of documents
 80    // o.keeptemp = true; // default is false, specifies whether to keep temp data
 81    // o.finalize = someFunc; // function called after reduce
 82    // o.scope = {}; // the scope variable exposed to map/reduce/finalize
 83    // o.jsMode = true; // default is false, force execution to stay in JS
 84    o.verbose = true; // default is false, provide stats on the job
 85    // o.out = {}; // objects to specify where output goes, by default is
 86                   // returned, but can also be stored in a new collection
 87                   // see: http://mongoosejs.com/docs/api.html#model_Model.mapReduce
 88    Person.mapReduce(o, function(err, results, stats) {
 89      console.log('map reduce took %d ms', stats.processtime);
 90      console.log(results);
 91      cleanup();
 92    });
 93  });
 94});
 95
 96function cleanup() {
 97  Person.remove(function() {
 98    mongoose.disconnect();
 99  });
100}