Node.js listen to MongoDB change

murvinlai picture murvinlai · Sep 28, 2011 · Viewed 8.2k times · Source

Is there a way for Node.js to listen to a change in a particular data in a collection of MongoDB, and fire an event if a change happens?

Answer

Brandon picture Brandon · Mar 6, 2013

Well, this is an old question, but I was struggling with the same thing. I found a number of tidbits that helped me put together a solution, and I've published it as a library:

https://github.com/TorchlightSoftware/mongo-watch

The library is written in coffeescript. Here's an example in javascript, for those that prefer.

var MongoWatch = require('mongo-watch'),
    watcher = new MongoWatch({parser: 'pretty'});

watcher.watch('test.users', function(event) {
  return console.log('something changed:', event);
});