2017-05-05 3 views
0

内の式のように定義する必要があります -集計フィールドをC#にMongoDBの集計クエリ下のポートしようとしているが、エラーになっオブジェクトエラー

コマンド集約に失敗しました: 定義されなければならないグループの集計フィールド「で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(); 
関連する問題