0

の列の値を合計したい場合は、列validatedAtstartDateendDateの間です。
だから私は結果(合計値)としてBigIntegerします。

これは私が試したものです:SpringデータMongoDBでSum SQLを使用するには?

 final List<AggregationOperation> aggregationOperations = new ArrayList<>(); 
     aggregationOperations.add(Aggregation.match(where("validatedAt").gte(startDate).lt(endDate))); 
     aggregationOperations.add(Aggregation.group().sum("price.value").as("total")); 

     final Aggregation turnoverAggregation = Aggregation.newAggregation(OrderEntity.class, aggregationOperations); 

     return this.mongoOperations.aggregate(turnoverAggregation, OrderEntity.class, BigInteger.class).getUniqueMappedResult(); 

これは動作しません。私はこのエラーがあります:

{"exception":"com.application.CommonClient$Builder$6","path":"/api/stats/dashboard","message":"Failed to instantiate java.math.BigInteger using constructor NO_CONSTRUCTOR with arguments ","error":"Internal Server Error","timestamp":1493209463953,"status":500} 

何か助けてください?

+0

何が問題なのですか?結果がありません?エラー? where条件だけでデータが返されますか? –

+0

私は自分の質問を編集しました。 – Anna

+0

Longを使ってみましたか? BigIntegerを期待する理由が分かりません –

答えて

1

あなただけのこのためのための新しいPOJOを追加する必要が `tを。マップするフィールドが増え、春に自動的にマップされるようにしたい場合に役立ちます。

問題を修正する正しい方法は、BasicDBObjectを使用することです.MongoDBはすべての値をキー値のペアとして保存するためです。

return this.mongoOperations.aggregate(turnoverAggregation, OrderEntity.class, BasicDBObject.class).getUniqueMappedResult().getInt("total"); 

サイドノート:金額はDouble/BigDecimalを使用してください。

+0

ニース:)ありがとう! – Anna

0

私はBigIntegerの属性を持つクラスを作成することで、この問題を解決:

private class Total { 
    private int total; 
} 

return this.mongoOperations.aggregate(turnoverAggregation, OrderEntity.class, Total.class).getUniqueMappedResult(); 
+0

これは通常の 'int'であり、' BigInteger'ではありません。 –

関連する問題