如何在Java中编写这个MongoDB的聚合查询?

huangapple go评论69阅读模式
英文:

How can I code this Aggregate MongoDB query in Java?

问题

我有这个MongoDB查询:

db.collection.aggregate([
  {
    $set: {
      AllSeasons: {
        $concatArrays: [
          "$Seasons",
          [ "Last season" ],
          [ "Last season 2" ]
        ]
      }
    }
  },
  { $set: { average: { $avg:  "$AllSeasons.goals"  } } }
])

我想在Java中编写这个查询。我尝试了几次,但在$concatArrays处没有获得错误,并且无法解决它。

这是我尝试过的代码:

AggregateIterable<Document> doc = coll.aggregate(Arrays.asList(
				new Document("$set", new Document("AllSeasons", new Document("$concatArrays", Arrays.asList("$Seasons", Arrays.asList("Last season"), Arrays.asList("Last season 2"))))),
				new Document("$set", new Document("Average", new Document("$avg", "$AllSeasons.goals")))));

那么,我该如何在Java中编写这个查询呢?

非常感谢!

英文:

I have this MongoDB query:

db.collection.aggregate([
  {
    $set: {
      AllSeasons: {
        $concatArrays: [
          &quot;$Seasons&quot;,
          [ &quot;$Last season&quot; ],
          [ &quot;$Last season 2&quot; ]
        ]
      }
    }
  },
  { $set: { average: { $avg:  &quot;$AllSeasons.goals&quot;  } } }
])

I want to code this query in Java. I have made several attempts but I don't get an error with $concatArrays and can't resolve it.

This is what I tried:

AggregateIterable&lt;Document&gt; doc = coll.aggregate(Arrays.asList(
				new Document(&quot;$set&quot;, new Document(&quot;AllSeasons&quot;, new Document(&quot;$concatArrays&quot;, &quot;[$Seasons, [&#39;$Last season&#39;] , [&#39;$Last season 2&#39;]] &quot;))),
				new Document(&quot;$set&quot;, new Document(&quot;Average&quot;, new Document(&quot;$avg&quot;, &quot;$AllSeasons.goals&quot;)))));

So how do I code this query in Java?

Thank you so much!

答案1

得分: 2

希望Mongo查询正常工作。您可以使用mongoTemplate进行操作。

public List<Object> test(){

    Aggregation aggregation = Aggregation.newAggregation(
        a -> new Document("$set",
                new Document("AllSeasons",
                    new Document("$concatArrays",
                        Arrays.asList("$Seasons", Arrays.asList("$Last_season_1"), Arrays.asList("$Last_season_2"))
                    )
                )
        ),
        b -> new Document("$set",
                new Document("average",
                    new Document("$avg","$AllSeasons.goals")
                )
        )
    ).withOptions(AggregationOptions.builder().allowDiskUse(Boolean.TRUE).build());

    return mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(YOUR_COLLECTION.class), Object.class).getMappedResults();
}
英文:

Hope the mongo query is working. And you can go with mongoTemplate

public List&lt;Object&gt; test(){

    Aggregation aggregation = Aggregation.newAggregation(
        a-&gt; new Document(&quot;$set&quot;,
				new Document(&quot;AllSeasons&quot;,
					new Document(&quot;$concatArrays&quot;,
						Arrays.asList(&quot;$Seasons&quot;,Arrays.asList(&quot;$Last_season_1&quot;),Arrays.asList(&quot;$Last_season_2&quot;))
					)
				)
		),
		b-&gt; new Document(&quot;$set&quot;,
				new Document(&quot;average&quot;,
					new Document(&quot;$avg&quot;,&quot;$AllSeasons.goals&quot;)
				)
		)
    ).withOptions(AggregationOptions.builder().allowDiskUse(Boolean.TRUE).build());

    return mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(YOUR_COLLECTION.class), Object.class).getMappedResults();
}

huangapple
  • 本文由 发表于 2020年9月15日 16:17:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63897803.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定