aggregate.js 2.8 KB

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