$function method help

hello, I am doing an aggregate query using $function to calculate a date and save it in another format

Order.aggregate([
  {
     $addfields: {
          newField: {
              $function: {
                 body: function(vistaDate){return moment(vistaDate).formato("YYYY-MM-DD")},
                arg: ["$vistaDate"],
                lang: "js"
            }
        }
    }
}
])

Error

MongoError: PlanExecutor error during aggregation :: caused by :: ReferenceError: _momentTimezone is not defined : body@:2:24

Thanks
L.

You should be able to do this with $dateToString.

This kind of cosmetic formatting are better if done in the client code. stateless client code is easier to scale.

Greetings, in my case $dateFromString is more adapted but it throws me the following error

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.