2016-11-09 51 views
1

任何人都可以将该查询转换为mongo for java(org.springframework.data.mongodb.core.MongoOperations)???将查询mongo转换为spring Mongooperations

我一直无法使用Java进行搜索。

谢谢。

db.respostas.aggregate([ 
    { 
     "$group":{ 
     "_id":{ 
      "per":"$pergunta", 
      "res":"$resposta" 
     }, 
     "respostaCount":{ 
      "$sum":1 
     } 
     } 
    }, 
    { 
     "$group":{ 
     "_id":"$_id.per", 
     "respostas":{ 
      "$push":{ 
       "resposta":"$_id.res", 
       "count":"$respostaCount" 
      } 
     }, 
     "count":{ 
      "$sum":"$respostaCount" 
     } 
     } 
    }, 
    { 
     "$sort":{ 
     "count":-1 
     } 
    } 
]) 
+0

请添加你到目前为止尝试过的。 – Veeram

+0

什么我就是这样,因为我不知道该怎么办@Veeram:'聚集AGG = newAggregation( \t \t \t组( “pergunta”, “resposta”) \t \t \t \t .Count中()作为。 ( “respostaCount”), \t \t \t组(), \t \t \t排序(Sort.Direction.DESC, “计数”) \t);' – Laisson

+0

请加样本文档 – Veeram

回答

1

你可以尝试这样的事情。

Aggregation agg = newAggregation(
      group(fields().and("per", "$pergunta").and("res", "$resposta")).count().as("respostaCount"), 
      group(fields("$_id.per")).push(new BasicDBObject("resposta", "$_id.res").append("count", "$respostaCount")) 
        .as("respostas").sum("respostaCount").as("count"), 
      sort(Sort.Direction.DESC, "count")); 
+0

展,完美地工作! Tks – Laisson

+0

经过一些测试后,我看到信息是正确的。我做了一些改变,是这样的:。 '聚合AGG = newAggregation( \t \t \t组(字段()和( “每”, “$ pergunta”)和( “RES”, “$ resposta”)) .count()。as(“respostaCount”) \t \t \t group(fields(“$ _ id.per”))。push(new BasicDBObject(“resposta”,“$ _id.res”)。append(“count ”, “$ respostaCount”))为( “respostas”), \t \t \t排序(Sort.Direction.DESC, “计数”));' 它缺乏所有的答案只有总和,我试着在最后放置'.sum(“$ respostaCount”)。as(“count”)',但它不起作用,然后删除。 – Laisson

+0

好的。我马上看看并更新答案。 – Veeram