聚合字段必须定义为对象错误中的表达式

Aggregate field must be defined as an expression inside an object error

尝试将 mongodb 聚合查询移植到 c# 但出现错误 -

Command aggregate failed: the group aggregate field 'ProductType' must be defined as an expression inside an object.

想知道 c# 代码有什么问题吗?

db.collectionName.aggregate([
        { $match: activeTrial},
        {  $project: {_id:1, CustomerType: 1, ProductType: 1} },
        { $group: {_id: {"cust": "$CustomerType", "prod" : "$ProductType"}, count: {$sum: 1}}},
          ]).toArray()

collectionName.Aggregate()
                .Match(filter)
                .Project(x => new {x.CustomerType, x.SubscriptionId, x.ProductType})
                .Group(x => new { x.ProductType, x.CustomerType }, g => new ActiveTrialsByProduct()
                {
                    ProductType = g.Key.ProductType,
                    CustomerType = g.Key.CustomerType,
                    ActiveTrials = g.Count()
                }).ToList();

这是合集..

{  
    "CustomerType" : "CustA",   
    "ProductType" : "ProdA", 
}
{  
    "CustomerType" : "CustA",   
    "ProductType" : "ProdB", 
}
{  
    "CustomerType" : "CustA",   
    "ProductType" : "ProdB", 
}
{  
    "CustomerType" : "CustA",   
    "ProductType" : "ProdA", 
}

组方法不知道g.Key.ProductType所以我们必须在项目方法中投影键的元素。

collectionName.Aggregate()
                .Match(filter)
                .Project(x => new {x.CustomerType, x.ProductName, x.SubscriptionId })
                .Group(x => new { x.ProductName, x.CustomerType }, g => new 
                {
                    Id = g.Key,
                    ActiveTrials = g.Count()
                })
                .Project(x => new ActiveTrialsByProduct()
                {
                    CustomerType = x.Id.CustomerType,
                    Product = x.Id.ProductName,
                    ActiveTrials = x.ActiveTrials
                }).ToList();