Reactive tables / Mongo views in Meteor

I need to update in the Mongo database some data based on changes in other tables. What I seem to be needing is reactivity within the database (or some kind of access to views)

Let’s consider the following example

The stores collection

{ store : "001", manager : "A" }
{ store : "002", manager : "A" }

The sales collection

{ store : "001", sales : 10 }
{ store : "002", sales : 11 }

The sales per manager collection

{ manager : "A", sales : 21 }

If the store collection is modified in such a way store “002” is now managed by manager “B”, then the sales per manager collection needs to be updated.

My understanding is that Mongo supports views (https://docs.mongodb.com/manual/core/views/) but I don’t know how to create them in Meteor, where to put my flow code and what to do if the computation of the view requires some more complex code that cannot be simply put into a mongo flow [$project, $group, ...]

I’m pretty certain MongoDB views cannot currently be managed through the current nodejs driver (which Meteor uses). However, views can be created through the mongo shell.

What I have not yet tried is whether a shell-created view appears as a standard collection to Meteor. I’d be interested to know whether that works! :slight_smile:

My other option are server side observers.

table.find().observe({
  added: d => console.log('added value function', d._id),
  changed: (new_document, old_document) => console.log('changed value function', old_document, '->', new_document),
  removed: d =>console.log('removed value function', d._id)
})

I see two advantages in using views however

  • my understanding is views are computed on demand (I don’t know if they are cached)
  • last time I tried observers on the server, it consumed a lot of memory and ended killing my server

I could try to use a normal Mongo collection to cache the view myself and hide it from the rest of the Meteor application.