Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var agg = db.GetCollection<Order>("orders").Aggregate();
- var project = agg.Project(o => new {o.Value
- , o.Product
- , Month = o.Date.Month
- , Year = o.Date.Year});
- var group = project.Group(
- key => new { key.Month, key.Product},
- g => new OrderSummary {Month = g.Key.Month
- ,Product = g.Key.Product
- , TotalSales = g.Sum(o => o.Value)});
- var result = group.ToListAsync().Result;
- public class Order : Entity
- {
- public DateTime Date { get; set; }
- public string Product { get; set; }
- public double Value { get; set; }
- }
- public class OrderSummary
- {
- public string Product { get; set; }
- public int Month { get; set; }
- public int Year { get; set; }
- public double TotalSales { get; set; }
- }
- { "aggregate" : "Order",
- "pipeline" : [
- { "$project" : { "Value" : "$Value", "Product" : "$Product", "Month" : { "$month" : "$Date" }, "Year" : { "$year" : "$Date" }, "_id" : 0 } }
- , { "$group" : {
- "_id" : { "Month" : "$Month", "Product" : "$Product" }
- , "Month" : "$Month"
- , "Product" : "$Product"
- , "TotalSales" : { "$sum" : "$Value" } } }]
- , "cursor" : { } }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement