Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. var agg = db.GetCollection<Order>("orders").Aggregate();
  2.  
  3. var project = agg.Project(o => new {o.Value
  4. , o.Product
  5. , Month = o.Date.Month
  6. , Year = o.Date.Year});
  7.  
  8. var group = project.Group(
  9. key => new { key.Month, key.Product},
  10. g => new OrderSummary {Month = g.Key.Month
  11. ,Product = g.Key.Product
  12. , TotalSales = g.Sum(o => o.Value)});
  13.  
  14. var result = group.ToListAsync().Result;
  15.  
  16. public class Order : Entity
  17. {
  18.  
  19. public DateTime Date { get; set; }
  20.  
  21. public string Product { get; set; }
  22.  
  23. public double Value { get; set; }
  24. }
  25. public class OrderSummary
  26. {
  27. public string Product { get; set; }
  28. public int Month { get; set; }
  29. public int Year { get; set; }
  30. public double TotalSales { get; set; }
  31.  
  32. }
  33.  
  34. { "aggregate" : "Order",
  35. "pipeline" : [
  36. { "$project" : { "Value" : "$Value", "Product" : "$Product", "Month" : { "$month" : "$Date" }, "Year" : { "$year" : "$Date" }, "_id" : 0 } }
  37. , { "$group" : {
  38. "_id" : { "Month" : "$Month", "Product" : "$Product" }
  39. , "Month" : "$Month"
  40. , "Product" : "$Product"
  41. , "TotalSales" : { "$sum" : "$Value" } } }]
  42. , "cursor" : { } }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement