PageRenderTime 15ms CodeModel.GetById 8ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/node_modules/mongoose/examples/population/population-options.js

https://bitbucket.org/coleman333/smartsite
JavaScript | 138 lines | 99 code | 18 blank | 21 comment | 6 complexity | 09787ea4f590572e640dfb4315e1bd09 MD5 | raw file
  1
  2var mongoose = require('../../lib');
  3var Schema = mongoose.Schema;
  4
  5console.log('Running mongoose version %s', mongoose.version);
  6
  7/**
  8 * Console schema
  9 */
 10
 11var consoleSchema = Schema({
 12  name: String,
 13  manufacturer: String,
 14  released: Date
 15});
 16var Console = mongoose.model('Console', consoleSchema);
 17
 18/**
 19 * Game schema
 20 */
 21
 22var gameSchema = Schema({
 23  name: String,
 24  developer: String,
 25  released: Date,
 26  consoles: [{
 27    type: Schema.Types.ObjectId,
 28    ref: 'Console'
 29  }]
 30});
 31var Game = mongoose.model('Game', gameSchema);
 32
 33/**
 34 * Connect to the console database on localhost with
 35 * the default port (27017)
 36 */
 37
 38mongoose.connect('mongodb://localhost/console', function(err) {
 39  // if we failed to connect, abort
 40  if (err) throw err;
 41
 42  // we connected ok
 43  createData();
 44});
 45
 46/**
 47 * Data generation
 48 */
 49
 50function createData() {
 51  Console.create(
 52    {
 53      name: 'Nintendo 64',
 54      manufacturer: 'Nintendo',
 55      released: 'September 29, 1996'
 56    },
 57    {
 58      name: 'Super Nintendo',
 59      manufacturer: 'Nintendo',
 60      released: 'August 23, 1991'
 61    },
 62    {
 63      name: 'XBOX 360',
 64      manufacturer: 'Microsoft',
 65      released: 'November 22, 2005'
 66    },
 67    function(err, nintendo64, superNintendo, xbox360) {
 68      if (err) return done(err);
 69
 70      Game.create(
 71        {
 72          name: 'Legend of Zelda: Ocarina of Time',
 73          developer: 'Nintendo',
 74          released: new Date('November 21, 1998'),
 75          consoles: [nintendo64]
 76        },
 77        {
 78          name: 'Mario Kart',
 79          developer: 'Nintendo',
 80          released: 'September 1, 1992',
 81          consoles: [superNintendo]
 82        },
 83        {
 84          name: 'Perfect Dark Zero',
 85          developer: 'Rare',
 86          released: 'November 17, 2005',
 87          consoles: [xbox360]
 88        },
 89        function(err) {
 90          if (err) return done(err);
 91          example();
 92        }
 93      );
 94    }
 95  );
 96}
 97
 98/**
 99 * Population
100 */
101
102function example() {
103  Game
104    .find({})
105    .populate({
106      path: 'consoles',
107      match: {manufacturer: 'Nintendo'},
108      select: 'name',
109      options: {comment: 'population'}
110    })
111    .exec(function(err, games) {
112      if (err) return done(err);
113
114      games.forEach(function(game) {
115        console.log(
116          '"%s" was released for the %s on %s',
117          game.name,
118          game.consoles.length ? game.consoles[0].name : '??',
119          game.released.toLocaleDateString()
120        );
121      });
122
123      return done();
124    });
125}
126
127/**
128 * Clean up
129 */
130
131function done(err) {
132  if (err) console.error(err);
133  Console.remove(function() {
134    Game.remove(function() {
135      mongoose.disconnect();
136    });
137  });
138}