mongodbmeteorgrounddb

Meteor subscription to get only new documents


Just wondering if there is a way to set my meteor subscription to load only new documents from a mongo collection, avoiding to sync deletes and updates (Since they are not relevant in the data that is shown to user).
Why I need that? It seems anytime I do a Meteor.subscribe after an offline period, the WHOLE collection is sent again from server to client, while I only need the new records.

I think this happen to keep local/remote database integrity, but since my app is planned to work online/offline (I'm using also groundDB), it seems to me It will be very inefficient in terms of data usage.

Thanks in advance.


Solution

  • You can create a publish which sends only new documents. Like:

    Meteor.publish('newDocumentsOnly', () => {
      let initializing = true;
      const handle = Collection.find().observeChanges({
        added: (id, fields) => {
          if (initializing) return;
          this.added('Collection', id, fields);
        }
      });
      initializing = false;
      this.ready();
      this.onStop(() => {
        handle.stop();
      });
    });