(InvalidPipelineOperator) Invalid $addFields :: caused by :: Unrecognized expression '$dateSubtract'

Unrecognized expression ‘$dateSubtract’ in aggregation.

{
    $addFields: {
     created_at: {
        $toDate: "$creationDate",
      },
       current_date: {
        $toDate: "$$NOW",
      },
    threshholdDate:{
      $dateSubtract:{
        startDate:"$$NOW",
        unit: "month",
        amount: 7,
        
      }
    },
    },
  },

(InvalidPipelineOperator) Invalid $addFields :: caused by :: Unrecognized expression ‘$dateSubtract’

As per documentation $dateSubtract is new in 5.0. Perhaps you are using an older version.

1 Like

You are right we are using version 4.4. Can you recommend some alternative please.

Solution I have came up with is. Any better solution will be appreciated.

 */
{
     created_at: {
        $toDate: "$creationDate",
      },
     threshholdDate:{
      $subtract: [ "$$NOW", 7*30*24*60*60*1000 ]
        
      }
  
    }
1 Like