arnaucube
arnaucube/thoughtsPublic Self-hosted repository
thoughts/node_modules/mongoose/examples/aggregate/aggregate.js
node_modules/mongoose/examples/aggregate/aggregate.jsRaw
  1
  2// import async to make control flow simplier
  3var async = require('async');
  4
  5// import the rest of the normal stuff
  6var mongoose = require('../../lib');
  7
  8require('./person.js')();
  9
 10var Person = mongoose.model('Person');
 11
 12// define some dummy data
 13var data = [
 14  {
 15    name: 'bill',
 16    age: 25,
 17    birthday: new Date().setFullYear((new Date().getFullYear() - 25)),
 18    gender: 'Male',
 19    likes: ['movies', 'games', 'dogs']
 20  },
 21  {
 22    name: 'mary',
 23    age: 30,
 24    birthday: new Date().setFullYear((new Date().getFullYear() - 30)),
 25    gender: 'Female',
 26    likes: ['movies', 'birds', 'cats']
 27  },
 28  {
 29    name: 'bob',
 30    age: 21,
 31    birthday: new Date().setFullYear((new Date().getFullYear() - 21)),
 32    gender: 'Male',
 33    likes: ['tv', 'games', 'rabbits']
 34  },
 35  {
 36    name: 'lilly',
 37    age: 26,
 38    birthday: new Date().setFullYear((new Date().getFullYear() - 26)),
 39    gender: 'Female',
 40    likes: ['books', 'cats', 'dogs']
 41  },
 42  {
 43    name: 'alucard',
 44    age: 1000,
 45    birthday: new Date().setFullYear((new Date().getFullYear() - 1000)),
 46    gender: 'Male',
 47    likes: ['glasses', 'wine', 'the night']
 48  }
 49];
 50
 51
 52mongoose.connect('mongodb://localhost/persons', function(err) {
 53  if (err) throw err;
 54
 55  // create all of the dummy people
 56  async.each(data, function(item, cb) {
 57    Person.create(item, cb);
 58  }, function(err) {
 59    if (err) {
 60      // handle error
 61    }
 62
 63    // run an aggregate query that will get all of the people who like a given
 64    // item. To see the full documentation on ways to use the aggregate
 65    // framework, see http://docs.mongodb.org/manual/core/aggregation/
 66    Person.aggregate(
 67      // select the fields we want to deal with
 68      {$project: {name: 1, likes: 1}},
 69      // unwind 'likes', which will create a document for each like
 70      {$unwind: '$likes'},
 71      // group everything by the like and then add each name with that like to
 72      // the set for the like
 73      {$group: {
 74        _id: {likes: '$likes'},
 75        likers: {$addToSet: '$name'}
 76      }},
 77      function(err, result) {
 78        if (err) throw err;
 79        console.log(result);
 80         /* [
 81         { _id: { likes: 'the night' }, likers: [ 'alucard' ] },
 82         { _id: { likes: 'wine' }, likers: [ 'alucard' ] },
 83         { _id: { likes: 'books' }, likers: [ 'lilly' ] },
 84         { _id: { likes: 'glasses' }, likers: [ 'alucard' ] },
 85         { _id: { likes: 'birds' }, likers: [ 'mary' ] },
 86         { _id: { likes: 'rabbits' }, likers: [ 'bob' ] },
 87         { _id: { likes: 'cats' }, likers: [ 'lilly', 'mary' ] },
 88         { _id: { likes: 'dogs' }, likers: [ 'lilly', 'bill' ] },
 89         { _id: { likes: 'tv' }, likers: [ 'bob' ] },
 90         { _id: { likes: 'games' }, likers: [ 'bob', 'bill' ] },
 91         { _id: { likes: 'movies' }, likers: [ 'mary', 'bill' ] }
 92         ] */
 93
 94        cleanup();
 95      });
 96  });
 97});
 98
 99function cleanup() {
100  Person.remove(function() {
101    mongoose.disconnect();
102  });
103}