statics.js 806 B

123456789101112131415161718192021222324252627282930313233343536373839
  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) 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. // using the static
  17. Person.findPersonByName('bill', function(err, result) {
  18. if (err) throw err;
  19. console.log(result);
  20. cleanup();
  21. });
  22. });
  23. });
  24. function cleanup() {
  25. Person.remove(function() {
  26. mongoose.disconnect();
  27. });
  28. }