将查询 mongo 转换为 spring Mongooperations

Convert query mongo to spring Mongooperations

任何人都可以将该查询转换为 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
      }
   }
])

你可以尝试这样的事情。

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"));