Mongodb : How to return total occurrence of user, but filtering out end_dates?


#1

How to filter out say user occurrence in MongodB, that is time dependent, so that it is forward looking and whenever end_date is exceeded, then user occurrence will go from

0 before 18:51:00
1, at 18:52:00
1 at 18:55:00
0 at 18:56:00
1 at 19:11:00
1 at 19:14:00
0 at 19:15:00

and

2 or more if there is any time overlap entries.

async function getAggregateUserIdCount(details) { // userid -> 'sec11'

    var promise = await new Promise(function (resolve, reject) {
        FutureTasks.aggregate([
            {
                $match: {
                    "userid": {$eq: details.userid},
                    
                 //"start_date": { $gt: ["$start_date", (moment(details.end_date).startOf('minute')) ] },
                  "start_date" : {$eq: (moment().startOf('minute')) }
                }
            },

            {
                $group: {
                    _id: "$userid", count: {$sum: 1}
                }
            }
        ], (err, db) => {
            err ? reject(err) : resolve(db);
        });
    });  
    return promise; // promise -> [ { _id: 'sec11', count: 1 } ] or []
}

getAggregateSparkUserIdCount(details)
            .then( txt => console.log('Time res : ', txt))

I get :

[ { _id: ‘sec11’, count: 9 } ]

But it is also including expired ( > end_date) so how to exclude items that are current?

I see that you can use TTL to remove but I dont want to remove until much much later due to audit reasons, but wanting to count the occurrence, maybe something like a scrolling window type of waveforms but records are still counted when already expired?

Maybe just need to take a sample time point and ignore everything before and after. Thanks

https://docs.mongodb.com/manual/tutorial/expire-data/