Skip to content

Instantly share code, notes, and snippets.

@jpaljasma
Last active December 5, 2019 23:07
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save jpaljasma/3260eaa6f7a40315863d to your computer and use it in GitHub Desktop.
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
/**
* 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);
});
@iest
Copy link

iest commented Feb 16, 2014

👍 for sharing

@abhiuser
Copy link

I am not able to get it working :(
I have created the capped collection. mongos> db.messages.isCapped() true
Do I need to install any npm ? I get the below error, When I try to run it.

Server started on local
app is listening on port 3000

/data/node_modules/mongodb/lib/utils.js:95
process.nextTick(function() { throw err; });
^
Error
at Error.MongoError (/data/node_modules/mongodb/node_modules/mongodb-core/lib/error.js:13:17)
at Cursor.sort (/data/node_modules/mongodb/lib/cursor.js:274:37)
at /data/app.js:207:55
at handleCallback (/data/node_modules/mongodb/lib/utils.js:93:12)
at /data/node_modules/mongodb/lib/cursor.js:392:5
at handleCallback (/data/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:223:5)
at Cursor.next (/data/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:507:5)
at /data/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:450:12
at queryCallback (/data/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:204:5)
at Callbacks.emit (/data/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:90:3)

@petergdoyle
Copy link

/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)

@slavahatnuke
Copy link

https://github.com/deoxxa/mongo-oplog-watcher/blob/master/index.js #oplog #watcher
you can modify this for any collection!

@ajaytlabs
Copy link

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