Help calculating and displaying time in min (not dates)


I’m working on a project where we want to measure and calculate how long it takes for a user to do a task. However, I’m having significant issues assigning a field as a ‘time’ type variable for displaying and calculating it.

For example, I want to measure how long it takes for a person from arriving to a scene to do a task. This would look like time 1 minus time 2, and then displaying the mean time from the whole database. This is measured in minutes.

The issue I see here, is that mongo charts seems not to have a ‘time’ formatting option, like it does for ‘date’ meaning any charts or calculation comes back as a null. Any tips or advice appreciated!


Hi @Andrew_Bivard -

While there isn’t a dedicated Charts feature for this, you should be able to accomplish this by using the $dateDiff operator in a calculated field or in the query bar.


1 Like

Thanks @tomhollander
Much appreciated, and thanks for linking the relevant documentation
With the data I have, it presents the error:
Unrecognized expression ‘$dateDiff’, correlationID = 177d4b3a2bdfaff536c06954

I’m not quite sure what’s causing this error.

What version is your cluster? As per the docs, this expression was added in MongoDB 5.0.