简体   繁体   English

春季在项目汇总上使用平均

[英]Using avg in spring on a project aggregation

I have the below query working as intended in mongo: 我有以下查询按预期在mongo中工作:

 db.test.aggregate([{$unwind: "$reports"}, {$project:  {reports: 1, avg: {$avg: "$reports.linesCount"}}}`])

However, I'm unable to convert this into Java via spring. 但是,我无法通过spring将其转换为Java。 You can't seem to run aggregation functions like avg on a ProjectionOperation, but it is possible on a GroupOperation. 您似乎无法在ProjectionOperation上运行像avg这样的聚合函数,但是可以在GroupOperation上运行。 There isn't an avg() method at all a ProjectionOperation. ProjectionOperation根本没有avg()方法。

Aggregation aggregation = Aggregation.newAggregation( 
                Aggregation.unwind("$reports"), 
                Aggregation.group(Fields.fields("_id"))
                .avg("reports.linesCount").as("avg"));

Any idea why this might be the case, when it clearly is possible in mongo? 知道在mongo中显然有可能时,为什么会这样吗? Is there some other way to translate my query into Java? 还有其他方法可以将我的查询翻译成Java吗?

Your query: {reports: 1, avg: {$avg: "$reports.linesCount"}} 您的查询: {reports: 1, avg: {$avg: "$reports.linesCount"}}

aggregates on reports.linesCount field, but your java code is trying to aggregate on .avg("reports.processingTime") 在聚集reports.linesCount领域,但你的java代码试图聚集在.avg("reports.processingTime")

So you might operate on wrong field. 因此,您可能在错误的字段上进行操作。 Check your query 检查您的查询

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM