You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
2.6 KiB

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