How to iteratively remove collections in a forEach loop without id?


#1

This is similar,

But I have a collection, depending on moment().Date() I will implement associate logic that sends

start job to machine 1, and then sends end job to machine 1
start job to machine 2, and then sends end job to machine 2

and so on, but how to remove rows without id when old rows have already expired?

id    task      start_date      end_date
0     12345     2016-05-10      2016-05-12
1     56789     2016-05-11      2016-05-18
etc

   FutureTasks.find().forEach(function (details) {

        if (moment(details.start_date) > moment().toDate()) {

            //Do Nothing

        } else if ( moment(details.start_date) < moment().toDate() &&
                    moment(details.end_date) > moment().toDate() ) {
            console.log('send start_task');
        }
        else if ( moment(details.start_date) < moment().toDate() &&
                    moment(details.end_date) < moment().toDate() ) {

            console.log('send end_task');
            // <- How to remove old row without _id?
        }
        else {
            console.log('end');
        }

        //Old expired tasks not removed???
        FutureTasks.remove({
                end_date: {$gt: new Date()},
        });
        console.log('end of ACS task, nothing detected in collection');

#2

Why can you not just use the details._id property? As it comes from the db, you have an _id property. Otherwise you can always simply clear out the expired jobs outside the forEach loop.

const now = new Date();
FutureTasks.remove({
  start_date: { $lt: now },
  end_date: { $lt: now },
});

The above is your logic translated to an selector. I guess you can leave out the start_date part and still have the same results. As logically a startdate lies always before the end date.

FutureTasks.remove({
  end_date: { $lt: new Date() },
});

#3

Thanks Stephen, but should be :

FutureTasks.remove({
  end_date: { $gt: new Date() },
});

as I want to remove old jobs when present time eventually passes end_date which is sometime in the future.

I mean:

FutureTasks.remove({
  details.end_date: { $gt: new Date() },
});

But how to do this inside the forEach iteration? Or the fact that

end_date: { $gt: new Date() },

iterates thorough the entire collection?

EDITED: added the remove function but not sure why rows are not removed when expired?

But actually you are correct Stephen, it should be less than, and not greater than.