thoughts/node_modules/mongoose/examples/replicasets/replica-sets.js
node_modules/mongoose/examples/replicasets/replica-sets.jsRaw
1
2// import async to make control flow simplier
3var async = require('async');
4
5// import the rest of the normal stuff
6var mongoose = require('../../lib');
7
8require('./person.js')();
9
10var Person = mongoose.model('Person');
11
12// define some dummy data
13var data = [
14 {
15 name: 'bill',
16 age: 25,
17 birthday: new Date().setFullYear((new Date().getFullYear() - 25))
18 },
19 {
20 name: 'mary',
21 age: 30,
22 birthday: new Date().setFullYear((new Date().getFullYear() - 30))
23 },
24 {
25 name: 'bob',
26 age: 21,
27 birthday: new Date().setFullYear((new Date().getFullYear() - 21))
28 },
29 {
30 name: 'lilly',
31 age: 26,
32 birthday: new Date().setFullYear((new Date().getFullYear() - 26))
33 },
34 {
35 name: 'alucard',
36 age: 1000,
37 birthday: new Date().setFullYear((new Date().getFullYear() - 1000))
38 }
39];
40
41
42// to connect to a replica set, pass in the comma delimited uri and optionally
43// any connection options such as the rs_name.
44var opts = {
45 replSet: {rs_name: 'rs0'}
46};
47mongoose.connect('mongodb://localhost:27018/persons,localhost:27019,localhost:27020', opts, function(err) {
48 if (err) throw err;
49
50 // create all of the dummy people
51 async.each(data, function(item, cb) {
52 Person.create(item, cb);
53 }, function(err) {
54 if (err) {
55 // handle error
56 }
57
58 // create and delete some data
59 var prom = Person.find({age: {$lt: 1000}}).exec();
60
61 prom.then(function(people) {
62 console.log('young people: %s', people);
63 }).then(cleanup);
64 });
65});
66
67function cleanup() {
68 Person.remove(function() {
69 mongoose.disconnect();
70 });
71}