Created
April 27, 2012 18:40
-
-
Save aheckmann/2511632 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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