mongoDB と spring でフィールドの平均を計算する方法。端末で使用する $avg() 関数がありますが、mongotemplate で実行する方法を説明します。
たとえば
db.sales.aggregate(
[
{
$group:
{
_id: "$item",
avgAmount: { $avg: { $multiply: [ "$price", "$quantity" ] } },
avgQuantity: { $avg: "$quantity" }
}
}
]
)
ここで平均を計算しているので、mongotemplate でどのように実行できますか。
今、関数を使用して平均評価を取得しています
私はこのような機能を使用しています..
public List getrating() {
TypedAggregation<RatingReviewModel> agg = newAggregation(RatingReviewModel.class,
group("hospitalid")
.avg("rating").as("avgrating")
);
AggregationResults<DBObject> result = operations.aggregate(agg, DBObject.class);
List<DBObject> resultList = result.getMappedResults();
return resultList;
}
ただし、デバッグ時にはresultListは空であるため、何も返されません。