2 // import async to make control flow simplier
3 var async = require('async');
5 // import the rest of the normal stuff
6 var mongoose = require('../../lib');
8 require('./person.js')();
10 var Person = mongoose.model('Person');
12 // define some dummy data
17 birthday: new Date().setFullYear((new Date().getFullYear() - 25))
22 birthday: new Date().setFullYear((new Date().getFullYear() - 30))
27 birthday: new Date().setFullYear((new Date().getFullYear() - 21))
32 birthday: new Date().setFullYear((new Date().getFullYear() - 26))
37 birthday: new Date().setFullYear((new Date().getFullYear() - 1000))
42 mongoose.connect('mongodb://localhost/persons', function(err) {
45 // create all of the dummy people
46 async.each(data, function(item, cb) {
47 Person.create(item, cb);
51 // when querying data, instead of providing a callback, you can instead
52 // leave that off and get a query object returned
53 var query = Person.find({age: {$lt: 1000}});
55 // this allows you to continue applying modifiers to it
56 query.sort('birthday');
59 // you can chain them together as well
60 // a full list of methods can be found:
61 // http://mongoosejs.com/docs/api.html#query-js
62 query.where('age').gt(21);
64 // finally, when ready to execute the query, call the exec() function
65 query.exec(function(err, results) {
76 Person.remove(function() {
77 mongoose.disconnect();