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.
Node and MongoDB work very-well together, in part because Mongo uses a JavaScript engine built into the database since JavaScript is good at handling JSON objects. Compared to other databases, such as MySQL, MongoDB is fast for storing certain types of data and can be automatically scaled.
MongoDB represents the data as a collection of documents rather than tables related by foreign keys. This makes it possible for the varied types of data dealt over the internet to be stored decently and accessed in the web applications using Node. js.
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 (http://nodejs.org/api.html#eventemitter-14).
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.
I would turn on replication in your mongodb. There is a replicate? database that contains a list of changes, similar to the mysql replication log. You can monitor that.
-daniel
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With