replica-sets.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // import async to make control flow simplier
  2. var async = require('async');
  3. // import the rest of the normal stuff
  4. var mongoose = require('../../lib');
  5. require('./person.js')();
  6. var Person = mongoose.model('Person');
  7. // define some dummy data
  8. var data = [
  9. { name : 'bill', age : 25, birthday : new Date().setFullYear((new
  10. Date().getFullYear() - 25)) },
  11. { name : 'mary', age : 30, birthday : new Date().setFullYear((new
  12. Date().getFullYear() - 30)) },
  13. { name : 'bob', age : 21, birthday : new Date().setFullYear((new
  14. Date().getFullYear() - 21)) },
  15. { name : 'lilly', age : 26, birthday : new Date().setFullYear((new
  16. Date().getFullYear() - 26)) },
  17. { name : 'alucard', age : 1000, birthday : new Date().setFullYear((new
  18. Date().getFullYear() - 1000)) },
  19. ];
  20. // to connect to a replica set, pass in the comma delimited uri and optionally
  21. // any connection options such as the rs_name.
  22. var opts = {
  23. replSet : { rs_name : "rs0" }
  24. };
  25. mongoose.connect('mongodb://localhost:27018/persons,localhost:27019,localhost:27020', opts, function (err) {
  26. if (err) throw err;
  27. // create all of the dummy people
  28. async.each(data, function (item, cb) {
  29. Person.create(item, cb);
  30. }, function (err) {
  31. // create and delete some data
  32. var prom = Person.find({age : { $lt : 1000 }}).exec();
  33. prom.then(function (people) {
  34. console.log("young people: %s", people);
  35. }).then(cleanup);
  36. });
  37. });
  38. function cleanup() {
  39. Person.remove(function() {
  40. mongoose.disconnect();
  41. });
  42. }