Search code examples
mongodbperlaggregatefastcgi

How to subtract minutes from ISODate() in perl and mongoDB?


Actually, I want to subtract minutes from my execution time field (subtract 5 minutes)... this is my mongodb collection :

{
    "_id" : ObjectId("5c4f11fae2fb4adc98f323ad"),
    "datetime" : ISODate("2019-01-28T14:30:18.423Z"),
    "queuename" : "indotama_advice",
    "payload" : "xxxxxxx",
    "executiontime" : ISODate("2019-01-28T14:31:18.423Z")
}

this is my code :

my $collection = $db->get_collection('schedulequeue');
$person =  $collection->find_one({"queuename" => "indotama_advice"},{"executiontime"=>1,"_id"=>0});
my $new_data = $collection->aggregate([
        {'$subtract' => ["executiontime", 5 * 60 * 1000]}
]);

but I got error message unrecognized pipeline stage name : $subtract, can anyone tell me the correct way to use $subtract in perl ??


Solution

  • As the error clearly states, $subtract is not an aggregation pipeline stage. Use $project to transform query results:

        $collection->aggregate(
            [
                {
                    '$project' => 
                        {
                            'alteredExecutionTime' => 
                                {
                                    '$subtract' => ['$executiontime', 5 * 60 * 1000]
                                }
                        }
                }
            ]
        );