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

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

How can I code this Aggregate MongoDB query in Java?

问题

我有这个MongoDB查询:

  1. db.collection.aggregate([
  2. {
  3. $set: {
  4. AllSeasons: {
  5. $concatArrays: [
  6. "$Seasons",
  7. [ "Last season" ],
  8. [ "Last season 2" ]
  9. ]
  10. }
  11. }
  12. },
  13. { $set: { average: { $avg: "$AllSeasons.goals" } } }
  14. ])

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

这是我尝试过的代码:

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

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

非常感谢!

英文:

I have this MongoDB query:

  1. db.collection.aggregate([
  2. {
  3. $set: {
  4. AllSeasons: {
  5. $concatArrays: [
  6. &quot;$Seasons&quot;,
  7. [ &quot;$Last season&quot; ],
  8. [ &quot;$Last season 2&quot; ]
  9. ]
  10. }
  11. }
  12. },
  13. { $set: { average: { $avg: &quot;$AllSeasons.goals&quot; } } }
  14. ])

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:

  1. AggregateIterable&lt;Document&gt; doc = coll.aggregate(Arrays.asList(
  2. 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;))),
  3. 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进行操作。

  1. public List<Object> test(){
  2. Aggregation aggregation = Aggregation.newAggregation(
  3. a -> new Document("$set",
  4. new Document("AllSeasons",
  5. new Document("$concatArrays",
  6. Arrays.asList("$Seasons", Arrays.asList("$Last_season_1"), Arrays.asList("$Last_season_2"))
  7. )
  8. )
  9. ),
  10. b -> new Document("$set",
  11. new Document("average",
  12. new Document("$avg","$AllSeasons.goals")
  13. )
  14. )
  15. ).withOptions(AggregationOptions.builder().allowDiskUse(Boolean.TRUE).build());
  16. return mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(YOUR_COLLECTION.class), Object.class).getMappedResults();
  17. }
英文:

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

  1. public List&lt;Object&gt; test(){
  2. Aggregation aggregation = Aggregation.newAggregation(
  3. a-&gt; new Document(&quot;$set&quot;,
  4. new Document(&quot;AllSeasons&quot;,
  5. new Document(&quot;$concatArrays&quot;,
  6. Arrays.asList(&quot;$Seasons&quot;,Arrays.asList(&quot;$Last_season_1&quot;),Arrays.asList(&quot;$Last_season_2&quot;))
  7. )
  8. )
  9. ),
  10. b-&gt; new Document(&quot;$set&quot;,
  11. new Document(&quot;average&quot;,
  12. new Document(&quot;$avg&quot;,&quot;$AllSeasons.goals&quot;)
  13. )
  14. )
  15. ).withOptions(AggregationOptions.builder().allowDiskUse(Boolean.TRUE).build());
  16. return mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(YOUR_COLLECTION.class), Object.class).getMappedResults();
  17. }

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:

确定