I'm very new to node and express but I'm trying to implement mongo-watch (https://github.com/TorchlightSoftware/mongo-watch) in my todo list application to listen to a changes to a mongodb collection. Now my question is where do I declare the watcher? In the controller? App.js? And what do I have to set"selection" and "idSet" to in order to just listen to insert events? I'm also using Angular.
var MongoWatch, watcher;
MongoWatch = require('mongo-watch');
watcher = new MongoWatch({
format: 'pretty'
});
watcher.query({
collName: collName,
selection: selection,
idSet: idSet
}, function(err, query) {
query.on('data', function(event) {
console.log('something changed:', event);
});
});
Any guidance would be appreciated!
Thanks!
You declare the watcher in the file that you want to respond its events.
From looking at the source code, it seems collName is the only required argument, so there should be no need to include the others.
I got it (finally!) working this way:
MongoWatch = require('mongo-watch');
watcher = new MongoWatch({
format: 'normal',
db: 'mydb' //important!
});
watcher.debug = console.log;
watcher.query(
{ collName: 'mycollection' }, //mydb.mycollection
function(err, query) {
if (err) console.log('Error: ', err);
query.on('data', function(event) {
console.log('something changed:', event) });
}
);