我在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
而且我是新上蒙戈,所以我會很感激任何提示建議就如何建立這些預測