0

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.

Ruben
  • 816
  • 1
  • 8
  • 21

1 Answers1

1

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();
  });
});
Mitar
  • 6,756
  • 5
  • 54
  • 86
  • Thank you. It works! Do you know if there is a way to observeChanges inside an array in the collection? I mean, detect any time a new object is pushed into an array... – Ruben Apr 11 '17 at 03:32
  • Then you should also observe `changed`. You can also do a diff manually, if you want to send only changes in array. But this becomes more complicated. – Mitar Apr 11 '17 at 04:06