2017-05-05 60 views
0

试图下面mongodb的聚合查询端口到c#,但得到的错误内的表达 -骨料字段必须被定义为对象错误

命令骨料失败:组聚集字段“ProductType”必须 被定义作为对象内的表达式。

有关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", 
} 
+0

请提供样本数据。这应该有助于重现错误。 – AxxE

+0

@AxxE更新了样本数据。谢谢。 – GBackMania

回答

1

组方法不知道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(); 
相关问题