2016-12-27 10 views
-2
db.flm_conversation.aggregate(
[ 
    {$match: {"conversationRecords.isPrimary":true,"conversationRecords.commentTime":{'$gte': new Date('2016-12-18 00:00:00'), '$lte': new Date('2016-12-18 23:59:59')} } }, 
    {$unwind:"$conversationRecords"}, 
    { $group: { 
     _id: { 
      "commentLevel": "$commentLevel", 
      "time":{"$add": [ 
       { 
        "$subtract": [ 
         { "$subtract": [ 
          "$conversationRecords.commentTime", 
          new Date(0) 
         ]} 
         , 
         { "$mod": [ 
          { "$subtract": [ 
           "$conversationRecords.commentTime", 
           new Date(0) 
          ]}, 
          1000 * 60 * 30 
         ]} 
        ] 
       }, 
       new Date(0) 
      ]} 
     }, 
     count: { "$sum": 1 } 
    }}, 
    { $group: {_id: "$_id.commentLevel",count: { "$sum": 1 },pointrecord:{$push: {time:"$_id.time",count:"$count"} } }}, 
    { $project: { _id: 1,count:1,pointrecord:1 } } 
]) 

Spring Mongodb集約APIを使用してこのクエリを変換する方法はありますか。Spring Mongodb集約APIを使用してクエリを変換する

AggregationOperation match = Aggregation.match(Criteria.where("conversationRecords.isPrimary").is(true) 
     .and("conversationRecords.commentTime").gte(DateUtils.stringToDate("2016-12-18 00:00:00","yyyy-MM-dd HH:mm:ss")).lte(DateUtils.stringToDate("2016-12-18 23:59:59","yyyy-MM-dd HH:mm:ss"))); 
AggregationOperation unwind = Aggregation.unwind("conversationRecords"); 
AggregationOperation group = Aggregation.group("commentLevel"); 
Aggregation agg = newAggregation(match,unwind,group); 
AggregationResults<SummaryRecordAggre> groupResults 
     = mongoTemplate.aggregate(agg, COLLECTION_NAME, SummaryRecordAggre.class); 

私は知らないグループ「$追加」に変換する方法?私は http://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongo.aggregation

+0

以下の同等であるあなたが嘆願は、あなたがこれまでにしようとしているものを追加することはできますか?誰かがあなたを助けてくれるのは簡単です。 – Veeram

+0

私の記述を変更しました。 – bnmjstu

答えて

0

によってあなたはこのような何かを試すことができます見つけます。私は必要ではないと感じた余分な演算子を削除しましたが、必要に応じて簡単に戻すことができます。

MongoDBオブジェクトを使用して現在すべての集約演算子として構築する必要がある最初のグループは、春のmongoではデフォルトでサポートされていません。

複数の値をプッシュできないので、2番目のグループのプッシュ演算子に似た方法です。

AggregationOperation match = Aggregation.match(Criteria.where("conversationRecords.isPrimary").is(true) 
     .and("conversationRecords.commentTime").gte(DateUtils.stringToDate("2016-12-18 00:00:00", "yyyy-MM-dd HH:mm:ss")).lte(DateUtils.stringToDate("2016-12-18 23:59:59", "yyyy-MM-dd HH:mm:ss"))); 
AggregationOperation unwind = Aggregation.unwind("conversationRecords"); 
AggregationOperation firstGroup = context -> context.getMappedObject(new BasicDBObject(
     "$group", new BasicDBObject(
     "_id", new BasicDBObject("commentLevel", "$commentLevel") 
     .append(
       "time", new BasicDBObject(
         "$subtract", new Object[]{"$conversationRecords.commentTime", new BasicDBObject(
         "$mod", new Object[]{"$conversationRecords.commentTime", 1000 * 60 * 30})}))).append("count", new BasicDBObject("$sum", 1)))); 
AggregationOperation secondGroup = Aggregation.group("_id.commentLevel").count().as("count").push(new BasicDBObject("time", "$_id.time").append("count", "$count")).as("pointrecord"); 
AggregationOperation project = Aggregation.project("_id", "count", "pointrecord"); 
Aggregation agg = newAggregation(match, unwind, firstGroup, secondGroup, project); 

これは、クエリ

[{ 
    "$match": { 
     "conversationRecords.isPrimary": true, 
     "conversationRecords.commentTime": { 
      "$gte": { 
       "$date": "2016-12-27T14:46:50.896Z" 
      }, 
      "$lte": { 
       "$date": "2016-12-27T14:46:50.896Z" 
      } 
     } 
    } 
}, { 
    "$unwind": "$conversationRecords" 
}, { 
    "$group": { 
     "_id": { 
      "commentLevel": "$commentLevel", 
      "time": { 
       "$subtract": ["$conversationRecords.commentTime", { 
        "$mod": ["$conversationRecords.commentTime", 1800000] 
       }] 
      } 
     }, 
     "count": { 
      "$sum": 1 
     } 
    } 
}, { 
    "$group": { 
     "_id": "$_id.commentLevel", 
     "count": { 
      "$sum": 1 
     }, 
     "pointrecord": { 
      "$push": { 
       "time": "$_id.time", 
       "count": "$count" 
      } 
     } 
    } 
}, { 
    "$project": { 
     "_id": 1, 
     "count": 1, 
     "pointrecord": 1 
    } 
}] 
} 
関連する問題