javamongodbmeteormeteor-collection-hooks

Meteor Collection-hooks not working for external DB modifications


I have a bunch of sensors scattered around.

These sensors transmit their status whenever they detect a change in their environment.

The data goes to a server(built using Java) where it processes that information then inserts it into a mongoDB.

My meteor App is essentially a dashboard for this information. I would like to do further processing on those entries as soon as they come in (analytics).

I started to use Collection-Hooks which works really nicely when the Meteor App makes changes to the database but not when the mongo Java-Driver does.

I need collection-hooks to detect new documents added to my mongoDB from the Java-driver. I'm also not married to collection-hooks, any other services suggested are welcomed.


Solution

  • What you want to use is an Observer on the cursor returned from a query: https://docs.meteor.com/api/collections.html#Mongo-Cursor-observe

    myCollection.find().observe({
      added(document) {
        // Do something with new document
      },
      changed(document) {
        // Update analytics in response to change
      },
      removed(oldDocument) {
        // Update analytics in response to change
      }
    });
    

    This will depend on the contents of the actual database, unlike collection hooks that only operate when Meteor code is called

    It's also worth noting that these hooks also track the specific query that was passed to find(). So if you only want to call these hooks for a specific subset of data, pass in the query like this this example from @scriptkid:

    var date = moment().utc().format("YYYY-MM-DD HH:mm:ss.SSS"); 
    log.find({ createdAt: { $gte: date } }).observe({
      added(document) { 
        console.log("new document added!"); 
      }, 
    });