2017-02-13 13 views
1

MongoDBの$group基準クエリを作成する方法:MongoDBのグループ集計のために

はMongoDBの $group aggretionもので
db.careLogBean.aggregate([{ 
    $unwind: "$comments" 
}, { 
    $sort: { 
     "comments.time": -1 
    } 
}, { 
    $group: { 
     _id: "$_id", 
     careGiverId: { 
      $first: "$careGiverId" 
     }, 
     careGiverName: { 
      $first: "$careGiverName" 
     }, 
     comments: { 
      $push: "$comments" 
     } 
    } 
}]) 

...どのようにJavaの基準 クエリ言語で記述する...

+0

これまでに試したことをお見せできますか? – Veeram

+0

遅く再生させて申し訳ありません。 –

+0

DBObject group = new BasicDBObject( "$ group"、新しいBasicDBObject( "_ id"、 "$ name"、 "$ salary")); –

答えて

1

あなたが試すことができますが集約以下。

import static org.springframework.data.domain.Sort.Direction.DESC; 
import static org.springframework.data.mongodb.core.aggregation.Aggregation.*; 

Aggregation aggregation = newAggregation(
     unwind("comments"), 
     sort(DESC, "comments.time"), 
     group("_id") 
      .first("careGiverId").as("careGiverId") 
      .first("careGiverName").as("careGiverName") 
      .push("comments").as("comments")); 

List<BasicDBObject> dbObjects = mongoOperations.aggregate(aggregation , collection_name, BasicDBObject.class).getMappedResults(); 

セットアップに基づいて最後のステートメントを調整することができます。

+0

ありがとうございましたveeram ... –

+0

今チェックしています。あなたは何が起こったのか知っています –

+0

haそれはうまく働いています。veeram –