var pipeline = collection.Aggregate() .Group(x => x.Field, g => new { Field = g.Key, Count = g.Count() }); var result = pipeline.ToList();
var pipeline = collection.Aggregate() .Match(x => x.Type == "A") .Group(x => x.Field, g => new { Field = g.Key, Sum = g.Sum(x => x.Value), Avg = g.Average(x => x.Value) }); var result = pipeline.ToList();This code creates an aggregate pipeline that first filters documents in the collection that have a "Type" field equal to "A". It then groups documents by the "Field" field and calculates the sum and average of the "Value" field in each group. The result is returned as a list of anonymous objects with "Field", "Sum", and "Avg" properties. Package library: MongoDB.Driver.