2016-11-09 4 views
1

誰でもそのクエリをmongo for java(org.springframework.data.mongodb.core.MongoOperations)に変換できますか????クエリmongoを春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を行う方法がわからないので、 – Veeram

+0

は、私が持っているもの、そういうことだ: '集約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"))。新しいBasicDBObject( "resposta"、 "$ _id.res")。append( "count()).count()。as(" respostaCount ")、 \t \t \tグループ」、 "$ respostaCount"))(のように。 "respostas")、 \t \t \tソート(Sort.Direction.DESC、 "カウント")); ' それは私がしようとしたすべての答え、の合計だけを欠いていました最後に '.sum(" $ respostaCount ")。as(" count ")'を入れますが、うまくいかずに削除されました。 – Laisson

+0

大丈夫。私は見て、すぐに答えを更新します。 – Veeram

関連する問題