0

mongoTempalteとAggregation.group()メソッドを使用して集約の汎用コードを書きたいと思います。対応する最初の演算子を持つジェネリックフィールドのリストをAggregation.groupメソッドに渡す方法

db.subscriberProfile.aggregate([{"$unwind":"$usage_history"}, 
{ "$group" : { "_id" :"$_id" ,"birthdate" : { "$first":"$birthdate"} , "category" : { "$first":"$category"} , "control_group" : { "$first":"$control_group"} , "sumOfTotalUsage" : { "$sum" :{"$cond": [ { "$gte" :[ "$usage_history.date" , ISODate("2017-01-13T10:43:55.306Z")] }, "$usage_history.total_usage", 0]}}}}, 
{ "$match" : { "$and" : [ { "birthdate" : { "$lte" : ISODate("2017-07-12T10:43:55.306Z")}} , { "birthdate" : { "$gte" : ISODate("1917-07-12T10:20:35.306Z")}} , { "category" : { "$in" : [ "Prepaid"]}} , { "control_group" : false} , { "sumOfTotalUsage" : { "$gte" : 0}}]}}]) 

そして、ここでは、Javaでの私のサンプルコードです:次のようにだから私はここに最初の()演算子

でグループ法に一般的なフィールドを渡すの問題は、私のデモネイティブクエリできました。

UnwindOperation unwind = Aggregation.unwind("usage_history"); 
GroupOperation group = Aggregation.group(fields.toArray(new String[fields.size()])).sum("usage_history.total_usage").as("sumOfTotalUsage"); 

$ first演算子を使用してグループ操作で複数のフィールドを追加する方法を知りたいだけです。 したがって、最初の演算子のリストを持つフィールドのリストをグループメソッドに渡す方法はありますか?このコードを試してみてください

+1

幸運を助けることを願っています!私たちはあなたが私たちとこれを共有してうれしいです:私はこれがフェイスブックやツイッターに追いつくのがより適切だと思う。一方、ここにあなたのための何かがあります:[良い質問をする方法](https://stackoverflow.com/help/how-to-ask) – Antoniossss

+1

このサイトはあなたのコードを書くつもりはありません – user7294900

+0

'Aggregation.groupのようなもの( "category")。as( "category")。sum( "usage_history.total_usage")。フィールドの値は、 ").as(" sumOfTotalUsage "); ' – Veeram

答えて

0

おかげで、私たちは指があなたのために交差し続けるだろう、これはあなたに

UnwindOperation unwind = Aggregation.unwind("usage_history"); 

    BasicDBObject object = new BasicDBObject("_id", "$_id"); 

    for (String string : fields) { 

    object.append(string, new BasicDBObject("$first", "$" + string)); 
    } 

    object.append("total", new BasicDBObject("$sum", new BasicDBObject("$cond", 
    new Object[] { new BasicDBObject("$gte", new Object[] { "$usage_history.date", calendarMin.getTime() }), 
     "$usage_history.total_usage", 0 }))); 

    BasicDBObject groupObject = new BasicDBObject("$group", object); 
    DBObject groupOperation = (DBObject) groupObject; 

    MatchOperation matchMain = Aggregation 
    .match(new Criteria().andOperator(criteriaList.toArray(new Criteria[criteriaList.size()]))); 

    Aggregation aggregation = Aggregation.newAggregation(unwind, new CustomGroupOperation(groupOperation), 
    matchMain); 
+0

実行中ですが、BasicDBObjectではなくAggregation.groupを通じてソリューションを探していました –

関連する問題