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.

84 lines
2.0 KiB

  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. // lean queries return just plain javascript objects, not
  55. // MongooseDocuments. This makes them good for high performance read
  56. // situations
  57. // when using .lean() the default is true, but you can explicitly set the
  58. // value by passing in a boolean value. IE. .lean(false)
  59. var q = Person.find({age: {$lt: 1000}}).sort('age').limit(2).lean();
  60. q.exec(function(err, results) {
  61. if (err) throw err;
  62. console.log('Are the results MongooseDocuments?: %s', results[0] instanceof mongoose.Document);
  63. console.log(results);
  64. cleanup();
  65. });
  66. });
  67. });
  68. function cleanup() {
  69. Person.remove(function() {
  70. mongoose.disconnect();
  71. });
  72. }