arnaucube
arnaucube/thoughtsPublic Self-hosted repository
thoughts/node_modules/mongoose/examples/globalschemas/gs_example.js
node_modules/mongoose/examples/globalschemas/gs_example.jsRaw
 1var mongoose = require('../../lib');
 2
 3
 4// import the global schema, this can be done in any file that needs the model
 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({
18    name: 'bill',
19    age: 25,
20    birthday: new Date().setFullYear((new Date().getFullYear() - 25))
21  }, function(err, bill) {
22    if (err) {
23      throw err;
24    }
25    console.log('People added to db: %s', bill.toString());
26    Person.find({}, function(err, people) {
27      if (err) {
28        throw err;
29      }
30
31      people.forEach(function(person) {
32        console.log('People in the db: %s', person.toString());
33      });
34
35      // make sure to clean things up after we're done
36      setTimeout(function() {
37        cleanup();
38      }, 2000);
39    });
40  });
41});
42
43function cleanup() {
44  Person.remove(function() {
45    mongoose.disconnect();
46  });
47}