2016-03-07 123 views
3

我在Mongo中進行聚合,我無法獲得預期的結果; 我想計數那些空列和那些非空,但顯然 {「$ ne」:[「$ RequestedOn」,null]}總是如此。Mongo C#驅動程序與非空字段集合不過濾

我真的不知道問題出在哪裏。 這是集合

/* 1 */ 
{ 
    "_id" : ObjectId("56cf03445667a09b17f661ef"), 
    "Name" : "User1 Test1", 
    "AccountRef" : "AccountRef1", 
    "Voucher" : "Voucher1",  
    "Email" : "[email protected]", 
    "CampaignId" : ObjectId("56c752d439bac5655eec7fb7") 
} 

/* 2 */ 
{ 
    "_id" : ObjectId("56cf034c5667a09b17f661f0"), 
    "Name" : "User2 Test2", 
    "AccountRef" : "AccountRef2", 
    "Voucher" : "Voucher2", 
    "Email" : "[email protected]", 
    "CampaignId" : ObjectId("56c752d439bac5655eec7fb7") 
} 

/* 3 */ 
{ 
    "_id" : ObjectId("56cf03565667a09b17f661f1"), 
    "Name" : "User3 Test3", 
    "AccountRef" : "AccountRef3", 
    "Voucher" : "Voucher3", 
    "GuidLink" : "7f079244-d94f-5e4a-8096-59b6df3ef64f", 
    "Email" : "[email protected]", 
    "CampaignId" : ObjectId("56c752d439bac5655eec7fb7") 
} 

/* 4 */ 
{ 
    "_id" : ObjectId("56cf035e5667a09b17f661f2"), 
    "Name" : "User4 Test4", 
    "AccountRef" : "AccountRef4", 
    "Voucher" : "Voucher4", 
    "Email" : "[email protected]", 
    "RequestedOn" : ISODate("2016-02-29T22:49:20.201Z"), 
    "CampaignId" : ObjectId("56c752d439bac5655eec7fb7") 
} 

的數據和我這是怎麼構建查詢

var wlUsers=Database.Collection; 
var cs = wlUsers.AsQueryable() 
.Where(wl => wl.CampaignId == id) 
.Select(p=> new { id = p.CampaignId, p.RequestedOn}) 
.GroupBy(wl => wl.id) 
.Select(g => new CampaignStatistics{ 
    ExistingCustomers = g.Count(),       
    TotalOrdered = g.Sum(w => w.RequestedOn.HasValue ? 1 :0), 
    LastOrder = g.Max(w => w.RequestedOn), 
    FirstOrder = g.Min(w => w.RequestedOn), 
    Last24hOrders = g.Sum(w => 
     w.RequestedOn > DateTime.Today.AddDays(-1)?1:0), 
}); 

結果是

{ 
    "ExistingCustomers" : 4, 
    "TotalOrdered" : 4, 
    "LastOrder" : ISODate("2016-02-29T22:49:20.201Z"), 
    "FirstOrder" : ISODate("2016-02-29T22:49:20.201Z"), 
    "Last24hOrders" : 4 
} 

但應

{ 
    "ExistingCustomers" : 4, 
    "TotalOrdered" : 1, 
    "LastOrder" : ISODate("2016-02-29T22:49:20.201Z"), 
    "FirstOrder" : ISODate("2016-02-29T22:49:20.201Z"), 
    "Last24hOrders" : 0 
} 

所以tota lOrdered必須是1和「Last24hOrders」 = 0

而且我是新上蒙戈,所以我會很感激任何提示建議就如何建立這些預測

回答

1

我會懷疑這條線

 TotalOrdered = g.Sum(w => w.RequestedOn.HasValue ? 1 :0), 

嘗試改變:

 TotalOrdered = g.Sum(w => (w.RequestedOn.HasValue && w.RequestedOn.Value > DateTime.Min) ? 1 :0) 
1

THX profesor79 我種分類的方式。這就是我的做法

var project = agg.Project(r => 
    new{ 
     id = r.CampaignId, 
     Last24H = r.RequestedOn.Value > DateTime.Today.AddDays(-1) ? 1 : 0, 
     HasRequested = (r.RequestedOn.Value > new DateTime() ? 1 : 0), //for some reason has value does not work (it is always true) 
     Date = r.RequestedOn}); 

var match = project.Match(p => p.id == id); 

var group = match.Group(
    r => r.id, 
    g => new CampaignStatistics{ 
      ExistingCustomers = g.Count(), 
      TotalOrdered = g.Sum(p => p.HasRequested), 
      LastOrder = g.Max(w => w.Date), 
      FirstOrder = g.Min(w => w.Date), 
      Last24HOrders = g.Sum(p => p.Last24H)}); 
相關問題