aggregate.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. likes: ['movies', 'games', 'dogs']
  15. },
  16. {
  17. name: 'mary',
  18. age: 30,
  19. birthday: new Date().setFullYear((new Date().getFullYear() - 30)),
  20. gender: 'Female',
  21. likes: ['movies', 'birds', 'cats']
  22. },
  23. {
  24. name: 'bob',
  25. age: 21,
  26. birthday: new Date().setFullYear((new Date().getFullYear() - 21)),
  27. gender: 'Male',
  28. likes: ['tv', 'games', 'rabbits']
  29. },
  30. {
  31. name: 'lilly',
  32. age: 26,
  33. birthday: new Date().setFullYear((new Date().getFullYear() - 26)),
  34. gender: 'Female',
  35. likes: ['books', 'cats', 'dogs']
  36. },
  37. {
  38. name: 'alucard',
  39. age: 1000,
  40. birthday: new Date().setFullYear((new Date().getFullYear() - 1000)),
  41. gender: 'Male',
  42. likes: ['glasses', 'wine', 'the night']
  43. }
  44. ];
  45. mongoose.connect('mongodb://localhost/persons', function(err) {
  46. if (err) throw err;
  47. // create all of the dummy people
  48. async.each(data, function(item, cb) {
  49. Person.create(item, cb);
  50. }, function(err) {
  51. if (err) {
  52. // handle error
  53. }
  54. // run an aggregate query that will get all of the people who like a given
  55. // item. To see the full documentation on ways to use the aggregate
  56. // framework, see http://docs.mongodb.org/manual/core/aggregation/
  57. Person.aggregate(
  58. // select the fields we want to deal with
  59. {$project: {name: 1, likes: 1}},
  60. // unwind 'likes', which will create a document for each like
  61. {$unwind: '$likes'},
  62. // group everything by the like and then add each name with that like to
  63. // the set for the like
  64. {$group: {
  65. _id: {likes: '$likes'},
  66. likers: {$addToSet: '$name'}
  67. }},
  68. function(err, result) {
  69. if (err) throw err;
  70. console.log(result);
  71. /* [
  72. { _id: { likes: 'the night' }, likers: [ 'alucard' ] },
  73. { _id: { likes: 'wine' }, likers: [ 'alucard' ] },
  74. { _id: { likes: 'books' }, likers: [ 'lilly' ] },
  75. { _id: { likes: 'glasses' }, likers: [ 'alucard' ] },
  76. { _id: { likes: 'birds' }, likers: [ 'mary' ] },
  77. { _id: { likes: 'rabbits' }, likers: [ 'bob' ] },
  78. { _id: { likes: 'cats' }, likers: [ 'lilly', 'mary' ] },
  79. { _id: { likes: 'dogs' }, likers: [ 'lilly', 'bill' ] },
  80. { _id: { likes: 'tv' }, likers: [ 'bob' ] },
  81. { _id: { likes: 'games' }, likers: [ 'bob', 'bill' ] },
  82. { _id: { likes: 'movies' }, likers: [ 'mary', 'bill' ] }
  83. ] */
  84. cleanup();
  85. });
  86. });
  87. });
  88. function cleanup() {
  89. Person.remove(function() {
  90. mongoose.disconnect();
  91. });
  92. }