将查询mongo转换为spring Mongooperations

时间:2016-11-09 23:01:54

标签: java sql mongodb aggregation-framework

任何人都可以将该查询转换为mongo for java(org.springframework.data.mongodb.core.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
      }
   }
])

1 个答案:

答案 0 :(得分: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"));
相关问题