arnaucube
arnaucube/thoughtsPublic Self-hosted repository
thoughts/node_modules/mongoose/examples/statics/statics.js
node_modules/mongoose/examples/statics/statics.jsRaw
 1var mongoose = require('../../lib');
 2
 3
 4// import the schema
 5require('./person.js')();
 6
 7// grab the person model object
 8var Person = mongoose.model('Person');
 9
10// connect to a server to do a quick write / read example
11
12mongoose.connect('mongodb://localhost/persons', function(err) {
13  if (err) {
14    throw err;
15  }
16
17  Person.create({name: 'bill', age: 25, birthday: new Date().setFullYear((new Date().getFullYear() - 25))},
18      function(err, bill) {
19        if (err) {
20          throw err;
21        }
22        console.log('People added to db: %s', bill.toString());
23
24        // using the static
25        Person.findPersonByName('bill', function(err, result) {
26          if (err) {
27            throw err;
28          }
29
30          console.log(result);
31          cleanup();
32        });
33      }
34  );
35});
36
37function cleanup() {
38  Person.remove(function() {
39    mongoose.disconnect();
40  });
41}