$inc not a group operator?

I have two functions, one is to find number of users, which works:

async function getAggregateSparkUserIdCount(userid) {
    var promise = await new Promise(function (resolve, reject) {
        FutureTasks.aggregate([
            {
                $match: {
                    "userid": {$eq: userid}
                }
            },

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

when there is no user this returns

[ ]

or when user sec123 appears 3 times

[ { _id: 'sec123', count: 3 } ]

I would like to decrement count by one, so in this case and not do anything otherwise…

[ { _id: 'sec123', count: 2 } ]

how to modify to decrement when it is saying:

Error reason in END_date logic is : { [MongoError: exception: unknown group operator ‘$inc’]

async function decrementUserIdCount(userid) {
    var promise = await new Promise(function (resolve, reject) {
        FutureTasks.aggregate(
            [
            {
                $match: {
                    "userid": {$eq: userid}
                }
            },
            {
                $group: {
                    _id: "$userid", count: {$inc: -1}
                }
            }       
        ]
            ,(err, db) => {
            err ? reject(err) : resolve(db);
        });
    }); 
    return promise;
}

This error message is accurate - $inc is not a valid group accumulator operator. You can see the full list of operators in the Mongo docs here.

Hi Hugh

Any idea how to decrement this object, which is constructed from the aggregate $group and $sum

Do you want to save the decremented count back in Mongo? If not, just decrement it after the data is retrieved from Mongo (after you get your cursor back just modify the returned data). If you want to update this data back in Mongo somewhere, then you’ll want to split this up into two Mongo queries (your aggregate query and an update query). Mongo aggregation also supports an $out operator. which let’s you store the result of your aggregation in a collection (this could help if you want to store the data changes back in Mongo).

It will be saved to Mongodb so that future in time I can decrement this value, until it is lowered to one, do my task, and then decrement one more time to zero. So that the cycle can start again from zero.

Not sure why the need for the $out operator when I need just aggregate to get number of times userA has appeared and then I just want to decrement this counter for userA. Because each user will have its associated frequency count of how many times it appeared in the MongodB collection?

Could you show both methods, decrementing without saving as well as saving thanks. Saving to collection is more interesting though for me.