使用非空字段对Mongo c#驱动程序进行聚合不会过滤

时间:2016-03-07 14:24:29

标签: c# mongodb mongodb-query aggregation-framework

我正在Mongo中进行聚合,我无法获得预期的结果; 我想计算那些带有空列的那些和那些非空但显然 {“$ ne”:[“$ RequestedOn”,null]}总是如此。

我无法确定问题出在哪里。 这是集合中的数据

/* 1 */
{
    "_id" : ObjectId("56cf03445667a09b17f661ef"),
    "Name" : "User1 Test1",
    "AccountRef" : "AccountRef1",
    "Voucher" : "Voucher1",    
    "Email" : "AccountRef1@server.ie",
    "CampaignId" : ObjectId("56c752d439bac5655eec7fb7")
}

/* 2 */
{
    "_id" : ObjectId("56cf034c5667a09b17f661f0"),
    "Name" : "User2 Test2",
    "AccountRef" : "AccountRef2",
    "Voucher" : "Voucher2",
    "Email" : "AccountRef1@server.ie",
    "CampaignId" : ObjectId("56c752d439bac5655eec7fb7")
}

/* 3 */
{
    "_id" : ObjectId("56cf03565667a09b17f661f1"),
    "Name" : "User3 Test3",
    "AccountRef" : "AccountRef3",
    "Voucher" : "Voucher3",
    "GuidLink" : "7f079244-d94f-5e4a-8096-59b6df3ef64f",
    "Email" : "AccountRef1@server.ie",
    "CampaignId" : ObjectId("56c752d439bac5655eec7fb7")
}

/* 4 */
{
    "_id" : ObjectId("56cf035e5667a09b17f661f2"),
    "Name" : "User4 Test4",
    "AccountRef" : "AccountRef4",
    "Voucher" : "Voucher4",
    "Email" : "AccountRef1@server.ie",
    "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
}

所以totalOrdered必须为1而'Last24hOrders'= 0。

此外,我是Mongo的新手,所以我将非常感谢有关如何构建这些预测的任何建议

2 个答案:

答案 0 :(得分:1)

我会怀疑这一行

<select id="test">
    <?php foreach($category as $category):?>
        <?php if($category['child_cat']){?>
            <optgroup label="<?php echo $category['name'];?>">
            <?php foreach($category['child_cat'] as $child):?>
                <option value="<?php echo $child['id'];?>"><?php echo $child['name'];?></option>
            <?php endforeach;?>
            </optgroup>
        <?php }else{ ?>
            <option value="<?php echo $category['id'];?>"><?php echo $category['name'];?></option>
        <?php } ?>
    <?php endforeach;?>
</select>

尝试更改为:

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

答案 1 :(得分: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)});