Why are so many documents getting fetched in Meteor?

92 views Asked by At

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' );
    });
});
0

There are 0 answers