How to query based on time-embedded JSON?


#1

Criteria: single job or multiple jobs can be submitted one at a time or as batch.

In MongodB I want to send async Promise jobs depending on the number of jobs already existing for that duration, so for example:

FIRST CASE (non-overlap time)

  1. First start_date appears at 12:00 and count is 0 (as there are prior user in database) so I expect start job to be sent and start_status will eventually settle to status code 200 (as returned by business logic)

  2. First end_date at 13:00 and count is at 1 so I expect end job to be sent and also settles to 200 and resets count back to 0.

SECOND CASE (overlap time)

  1. Second start_date appears at 14:00 and again sends job and expect start_status = 200, now count = 1, because although First Job already ended at 13:00 but it should be ignored since it is considered as past.

  2. Third start_date also appears at 16:00 but this is not sent as count = 1 from second job, but I want to increment stack count = 2 because now there are two jobs, so start_status for this job remains as 0

  3. Second end_job appears at 18:00 but this is not send as count = 2, but need to decrement count = 1, so end_status for second job remains at 0

  4. Third end_job appears at 21:00 and now it is sent because count = 1, end_status for this job = 200 and count = 0

Now it should be ready to cycle all over again, whether it is overlapping (meaning more than one job per user) or single non-lapping jobs.

I tried the below Mongo query but not sure if it is working because I have to wait for time to actually arrive before knowing whether it is working:

async function getAggregateSparkUserIdCountUp(details) { // userid -> 'sec11'
    var promise = await new Promise(function (resolve, reject) {
    FutureTasks.aggregate(
        [
            {
                $match: {
                    "userid": {$eq: details.userid},

                    $and: [
                        {"start_date": {$lte: moment(details.start_date).local().subtract(1, 'minutes').format("YYYY-MM-DD HH:mm:ss")}},
                        {"start_date": {$gte: moment(details.start_date).local().add(1, 'minutes').format("YYYY-MM-DD HH:mm:ss")}}
                    ]
                }
            },
            {
                $group: {
                    _id: "$userid", count: {$sum: 1}
                }
            }
        ]
        , (err, db) => {
            err ? reject(err) : resolve(db);
        });
});  

This is for the start_date, similar function would be for the end_date

Timing waveform would be