-
-
Save jpaljasma/3260eaa6f7a40315863d to your computer and use it in GitHub Desktop.
How to subscribe for new MongoDB documents in Node.js using tailable cursor
This file contains 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
/** | |
* How to subscribe for new MongoDB documents in Node.js using tailable cursor | |
*/ | |
// subscriber function | |
var subscribe = function(){ | |
var args = [].slice.call(arguments); | |
var next = args.pop(); | |
var filter = args.shift() || {}; | |
if('function' !== typeof next) throw('Callback function not defined'); | |
// connect to MongoDB | |
require('mongodb').MongoClient.connect('mongodb://localhost/test', function(err, db){ | |
// make sure you have created capped collection "messages" on db "test" | |
db.collection('messages', function(err, coll) { | |
// seek to latest object | |
var seekCursor = coll.find(filter).sort({$natural: -1}).limit(1); | |
seekCursor.nextObject(function(err, latest) { | |
if (latest) { | |
filter._id = { $gt: latest._id } | |
} | |
// set MongoDB cursor options | |
var cursorOptions = { | |
tailable: true, | |
awaitdata: true, | |
numberOfRetries: -1 | |
}; | |
// create stream and listen | |
var stream = coll.find(filter, cursorOptions).sort({$natural: -1}).stream(); | |
// call the callback | |
stream.on('data', next); | |
}); | |
}); | |
}); | |
}; | |
// new documents will appear in the console | |
subscribe( function(document) { | |
console.log(document); | |
}); |
https://github.com/deoxxa/mongo-oplog-watcher/blob/master/index.js #oplog #watcher
you can modify this for any collection!
its working only remove /* sort({$natural: -1}) */ from this code.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
/home/peter.doyle/node/streaming/node_modules/mongodb/lib/utils.js:97
process.nextTick(function() { throw err; });
^
MongoError: Tailable cursor doesn't support sorting
at Cursor.sort (/home/peter.doyle/node/streaming/node_modules/mongodb/lib/cursor.js:276:37)
at /home/peter.doyle/node/streaming/server.js:37:55
at handleCallback (/home/peter.doyle/node/streaming/node_modules/mongodb/lib/utils.js:95:12)
at /home/peter.doyle/node/streaming/node_modules/mongodb/lib/cursor.js:438:5
at handleCallback (/home/peter.doyle/node/streaming/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:234:5)
at nextFunction (/home/peter.doyle/node/streaming/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:604:5)
at /home/peter.doyle/node/streaming/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:538:7
at queryCallback (/home/peter.doyle/node/streaming/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:215:5)
at Callbacks.emit (/home/peter.doyle/node/streaming/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:84:3)
at null.messageHandler (/home/peter.doyle/node/streaming/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:219:23)