Your result is IEnumerable from BsonDocument, you can Serialize them to C # objects using BSonSerializer. And this piece of code just writes them to the console, but you can see that you typed objects
List<Average> returnValue = new List<Average>(); returnValue.AddRange(documents.Select(x=> BsonSerializer.Deserialize<Average>(x))); foreach (var obj in returnValue) { Console.WriteLine("Species {0}, avg weight: {1}",returnValue._Id,returnValue.AvgWeight); }
And then you have a class called Average where the property name matches the names in BSonDocument, if you want to rename that (because _Id is not so good in C # terms regarding naming conventions), you can add $ project BsonDocument to your pipeline .
public class Average { public string _Id { get; set; } public Double AvgWeight {get; set; } }
$ sample project (add this to your pipeline just before sorting
var project = new BsonDocument { { "$project", new BsonDocument { {"_id", 0}, {"Species","$_id"}, {"AvgWeight", "$AvgWeight"}, } } };
source share