Skip to content

Instantly share code, notes, and snippets.

@aheckmann
Created April 27, 2012 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aheckmann/2511632 to your computer and use it in GitHub Desktop.
Save aheckmann/2511632 to your computer and use it in GitHub Desktop.
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
mongoose.connect('localhost', 'test');
var schema = new Schema({
_id: Number
, name: String
// etc
});
var A = mongoose.model('A', schema);
mongoose.connection.on('open', function () {
var lastId = 0
, done = false;
function tail () {
var stream = A.find({ _id: { $gt: lastId }}).tailable().stream();
stream.on('data', function (doc) {
lastId = doc._id;
// do something with doc
console.log(doc);
});
stream.on('close', function (err) {
if (done) return;
console.log('restarting from %s', lastId);
tail();
});
stream.on('error', function (err) {
console.error('stream error:', err);
done = true;
disconnect();
})
}
tail();
});
function disconnect (err) {
if (err) console.error('error', err && err.stack || err);
console.error('disconnecting');
mongoose.disconnect();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment