gs_example.js 995 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. var mongoose = require('../../lib');
  2. // import the global schema, this can be done in any file that needs the model
  3. require('./person.js')();
  4. // grab the person model object
  5. var Person = mongoose.model("Person");
  6. // connect to a server to do a quick write / read example
  7. mongoose.connect('mongodb://localhost/persons', function(err) {
  8. if (err) throw err;
  9. Person.create({
  10. name : 'bill',
  11. age : 25,
  12. birthday : new Date().setFullYear((new Date().getFullYear() - 25))
  13. }, function (err, bill) {
  14. if (err) throw err;
  15. console.log("People added to db: %s", bill.toString());
  16. Person.find({}, function(err, people) {
  17. if (err) throw err;
  18. people.forEach(function(person) {
  19. console.log("People in the db: %s", person.toString());
  20. });
  21. // make sure to clean things up after we're done
  22. setTimeout(function () { cleanup(); }, 2000);
  23. });
  24. });
  25. });
  26. function cleanup() {
  27. Person.remove(function() {
  28. mongoose.disconnect();
  29. });
  30. }