How do i use two aggregations in a way that output of one is a input for another

Hello all,
I have two fields in my documents . one is called “start_time” and the other is “end_time”
They are both in string form and they are in UTC ISO format of dates eg 2022-06-15T02:59:31Z

My aim is find the difference between the two times (ie Duration)

So now what i should do is first convert my string fields to Date using “toDate” aggregate function and then use “dateDiff” to find the difference .


  $project:         {"duration":{ $dateDiff:{ startDate: {$toDate: "$start_time"},endDate: {$toDate: "$stop_time"},unit: "hour" }} }


So is this the right way to do it

Did you tried it? Does it worked? If you tried it and it does not work it would be nice to know what went wrong. Did you get a syntax error, a run time error, the wrong result or no result at all?

The most common error is querying the wrong field name. And it looks like it is what you did too.

You query

and your data is