mapreduce.js 2.7 KB

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