I want to subtract a saved time in a document from the current time and save it in a different field of that document. I tried $subtract which returns value in milliseconds and I tried to save it in a field with type Number but getting cast error.
Here is what I tried
abc.updateOne({ checklistId: req.query.checklistId }, { timeCompleted: { $subtract: [ new Date(), "$timeCreated" ] } }).then(() => {
console.log('time updated');
})
Error
CastError: Cast to number failed for value "{ '$subtract': [ 2019-05-14T07:24:24.751Z, '$timeCreated' ] }" at path "timeCompleted"