The aggregation for this is done in two steps:
$match - Retrieve documents with TimeStamp value between some defined minDate and maxDate.
$group - Group on null. This will put all documents in a single group so we can apply an accumulator function across everything from the step 1 $match. The accumulator functions you're looking for are $min, $max, and $avg.
IMongoCollection<Entity> collection = GetMyCollection();
DateTime minDate = default(DateTime); // define this yourself
DateTime maxDate = default(DateTime); // define this yourself
var match = new BsonDocument
{ {
"$match", new BsonDocument
{ {
"TimeStamp", new BsonDocument
{ {
"$and", new BsonDocument
{
{ "$gt", minDate },
{ "$lt", maxDate }
}
} }
} }
} };
var group = new BsonDocument
{ {
"$group", new BsonDocument
{
{ "_id", BsonNull.Value },
{ "min", new BsonDocument { { "$min", "Value" } } },
{ "max", new BsonDocument { { "$max", "Value" } } },
{ "avg", new BsonDocument { { "$avg", "Value" } } },
}
} };
var result = collection.Aggregate(PipelineDefinition<Entity, BsonDocument>.Create(match, group)).Single();
double min = result["min"].AsDouble;
double max = result["max"].AsDouble;
double avg = result["avg"].AsDouble;