I have a MongoDB collection of objects like this.
{
"_id" : "56fd034268f44e1eccb8a775",
"Period" : {
"StartDateTime" : ISODate("2016-04-02T06:00:00Z"),
"EndDateTime" : ISODate("2016-04-02T08:00:00Z")
},
... //other attributes here
}
{
"_id" : "56fd034968f44e1eccb8a776",
"Period" : {
"StartDateTime" : ISODate("2016-04-04T06:00:00Z"),
"EndDateTime" : ISODate("2016-04-04T07:30:00Z")
},
... //other attributes here
}
I want to create a strongly-typed aggregation query to compute the total elapsed time, for all time slices.
I wrote this statement.
var aggregate = coll.Aggregate()
.Project(r => new { duration = r.Period.EndDateTime - r.Period.StartDateTime })
.Group(k => 0, g => g.Sum(x => x.duration));
But I receive the following error.
Cannot implicitly convert type 'System.TimeSpan' to 'int'
Is there a solution? Have I to switch to BsonDocument approach?