1

My application is fetching a huge number of documents from the server in Meteor. I've tried optimizing the Live Query to reduce this, but I'm still getting several thousand documents per hour when the collection.find().count() is just 13.

enter image description here

Subscription:

Template.calendar.onCreated( function() {
    var self = this;
    self.ready = new ReactiveVar();
    PostSubs = new SubsManager();
    self.autorun(function() {
        var handle = PostSubs.subscribe('meals');
        self.ready.set(handle.ready());
    });
});

Publication:

Meteor.publish('meals', function() {
    return Meals.find({
        userId : this.userId,
        start : {
            $gte : moment().subtract(2, 'w').format("YYYY-MM-DD"),
            $lte : moment().add(2,'w').format("YYYY-MM-DD")
        }

    });
    return this.ready();
});

Usage in context:

Template.calendar.onRendered(() => {
    $( '#meals-calendar' ).fullCalendar({
        defaultView: 'basicWeek',
        firstDay: 1,
        height: 200,
        editable: true,
        events( start, end, timezone, callback ) {
            let data = Meals.find().fetch().map( ( event ) => {
                return event;
            });

            if ( data ) {
                callback( data );
            }
        },
        eventRender( event, element ) {
            element.find( '.fc-content' ).html(
                `<strong>${ event.title }</strong>`
                );
        },
        dayClick( date ) {
            Session.set( 'eventModal', { type: 'add', date: date.format() } );
            $( '#add-edit-event-modal' ).modal( 'show' );
            $('#title').focus();
        },
        eventClick( event ) {
            Session.set( 'eventModal', { type: 'edit', event: event._id } );
            $( '#add-edit-event-modal' ).modal( 'show' );
        },
        eventDrop( event, delta, revert ) {
            let date = event.start.format();
            let update = {
                _id: event._id,
                start: date
            };

            Meteor.call( 'editEvent', update, ( error ) => {
                if ( error ) {
                    Bert.alert( error.reason, 'danger' );
                }
            });
        },
    });

    Tracker.autorun( () => {
        Meals.find().fetch();
        $( '#meals-calendar' ).fullCalendar( 'refetchEvents' );
    });
});
Chris
  • 644
  • 1
  • 12
  • 28
  • The question is lacking detail. How did you optimize livequery? How many different subscriptions are active (different user ids)? IIRC, multiplexing is applied to identical queries belonging to active publications with multiple subscribers. Are the components unsubscribing and re-subscribing repeatedly? This is not easy to determine the cause without understanding the dynamic behavior of your users and system and may be easier by finer grained tracking of system events. – MasterAM Jan 24 '17 at 22:27
  • @MasterAM Sorry to leave that out. At the time of the screenshot, there was only one subscriber. It's generating that many document reloads for a single user. – Chris Jan 24 '17 at 23:18
  • Are you sure that it is using the oplog? It is possible that poll-and-diff is used, or that the user is unsubscribing and re-subscribing in quick succession. – MasterAM Jan 24 '17 at 23:48
  • Did you solve this? Have a similar problem. no idea why we have so many documents getting published. – sathish Nov 24 '17 at 13:09

0 Answers0