2017-01-26 15 views
0

私はこの前に何百万回も質問されていますが、私のユースケースと一致するものは何も見つかりません。おそらく、Spring Mongo集約フレームワークの知識が不足しているためです。私は誰かが私のために主題についていくらかの光を放つことができるといい。mongo db query to spring

public List<DBObject> getAllAuditEvents(Integer page, Integer pageSize) { 
    List<DBObject> agg = new ArrayList(); 
    agg.add(BasicDBObject.parse("{$group: {_id: \"$corrId\", currentEvent: {\"$last\": \"$event.status\"}, events: { $push: \"$$ROOT\"} }}")); 
    agg.add(BasicDBObject.parse("{$sort: {\"timestamp\": -1} }")); 
    agg.add(BasicDBObject.parse("{$limit: " + pageSize + "}")); 
    agg.add(BasicDBObject.parse("{$skip: " + (page * pageSize) + " }")); 
    return (List<DBObject>) mongoTemplate.getCollection("mongoAuditEvent") 
     .aggregate(agg).results(); 
} 

しかし、私は持っているので、これは失敗します。

モンゴシェル集約

db.mongoAuditEvent.aggregate([ 
    {$group: {_id : "$corrId", currentEvent: {"$last": "$event.status"}, events: { $push: "$$ROOT"} }}, 
    {$sort: {"timestamp": 1} }, 
    {$skip: 0 }, 
    {$limit: 10} 
], {allowDiskUse: true}).pretty() 

{ 
    "_id" : "00aa9c60-2950-439b-976e-0980da829981", 
    "currentEvent" : "UPSTREAM_QUEUE", 
    "events" : [ 
    { 
     "_id" : "e746cd3f-dfe3-47b3-a1d0-3342adaf61c3", 
     "_class" : "no.fint.audit.plugin.mongo.MongoAuditEvent", 
     "corrId" : "00aa9c60-2950-439b-976e-0980da829981", 
     "source" : "employee", 
     "timestamp" : NumberLong("1484478431288"), 
     "event" : { 
     "corrId" : "00aa9c60-2950-439b-976e-0980da829981", 
     "action" : "GET_ALL_EMPLOYEES", 
     "status" : "DOWNSTREAM_QUEUE", 
     "time" : NumberLong("1484478431287"), 
     "source" : "employee", 
     "client" : "CACHE_SERVICE" 
     }, 
     "clearData" : true 
    } 
    ] 
} 

私は春に次にこれを翻訳するために管理を作成しますいいえtはどこでもallowDiskUse:trueオプションを指定しました。私はそれを指定する適切な場所を見つけることができません。

public List<MongoAuditEventGroup> getAllAuditEvents(Integer page, Integer pageSize) { 
    return mongoTemplate.aggregate(
     Aggregation.newAggregation(
      Aggregation.group("corrId"), 
      Aggregation.sort(Sort.Direction.DESC, "corrId"), 
      Aggregation.limit(pageSize), 
      Aggregation.skip((long)page * pageSize) 
     ).withOptions(newAggregationOptions().allowDiskUse(true).build()), 
      MongoAuditEvent.class, 
      MongoAuditEventGroup.class).getMappedResults(); 
} 

しかし、私はグループにcurrentEventを追加する方法のまわりで私の頭を包むように見えることはできません。

それから私は、そこに集計を指定するのがより好ましい方法だということを見ました。

誰かが私にここで与えることができる助けや指導にはとても感謝しています。

答えて

1

私はあなたのMongo Shellクエリを終了します。このようなことを試すことができます。

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

Aggregation agg = newAggregation(
       group("corrId").last("event.status").as("currentEvent").push("$$ROOT").as("events"), 
       sort(ASC, "timestamp"), 
       skip(0L), 
       limit(10L)). 
       withOptions(newAggregationOptions().allowDiskUse(true).build()); 

List<MongoAuditEventGroup> results = mongoTemplate.aggregate(agg, MongoAuditEvent.class, 
     MongoAuditEventGroup.class).getMappedResults(); 
+0

これはうまくいきました。 :-) ありがとうございました!しかし、これは私の出力クラスのための 'シリアライザが見つかりませんでした 'という問題を私に残します。私はここで何が欠けていますか? –

+0

Nevermind。出力クラスのプロパティでgetter/setterを忘れました。再びありがとう! –