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.

41 lines
882 B

  1. var mongoose = require('../../lib');
  2. // import the schema
  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) {
  9. throw err;
  10. }
  11. Person.create({name: 'bill', age: 25, birthday: new Date().setFullYear((new Date().getFullYear() - 25))},
  12. function(err, bill) {
  13. if (err) {
  14. throw err;
  15. }
  16. console.log('People added to db: %s', bill.toString());
  17. // using the static
  18. Person.findPersonByName('bill', function(err, result) {
  19. if (err) {
  20. throw err;
  21. }
  22. console.log(result);
  23. cleanup();
  24. });
  25. }
  26. );
  27. });
  28. function cleanup() {
  29. Person.remove(function() {
  30. mongoose.disconnect();
  31. });
  32. }