Monitoring Mongo for changes with Node.js

By : Josh K

I'm using Node.js for some project work and I would like to monitor my Mongo database (collection) for changes, basically fire an event if something gets added.

Anyone know if this is possible? I'm using the node-mongodb-native drivers.

If it's not I'd also like any available pointers on pushing data from the server (run with node) to the client browser.

By : Josh K


collection.insert({"key1":val1,"key2":"val2"}, function(err, info){
if(err){ //handle this } else{ if(info){

you call a fireandforgetfunction(info); here that can write to logs or send to SQS or do some other child spawn or in process thing. This could even be a callback but I think a fire and forget may do in most circumstances. I say fire and forget because I presume you don't need to hold up the response so you can return what ever you need to the client. And in part-answer to your other question you can return JSON like this

         var myJSON =[];                            
         sys.puts("Cool info stored and did a non blocking fire and forget for some other mongo monitoring stuff/process and sending control back to the browser");
         sys.puts(sys.inspect(info));//remove later
         myJSON.push({"key1":val1,"key2":val2});//or whatev you want to send
         res.writeHead(200, { "Content-Type" : "text/plain" });

Almost 3y since the last answer. I would suggest looking at:

npm install mubsub should get you there

By : dgorissen

The question is if all data is added to your database through your node.js app. If so, you can use the EventEmitter class of node.js to trigger an event (

If the database is populated by some other app, things are getting difficult. In this case you would need something like a database trigger, which is AFAIK not yet availabled in MongoDB.

Pushing Events to the Client (aka Comet) will be possible once the HTML 5 websockets API makes its way into all major browsers.

In the meantime, you can only try to emulate this behaviour using techniques like (long-term) AJAX polling, forever frame etc. but each of them has its weaknesses.

This video can help you solving your question :)
By: admin