2016-12-15 4 views
0

私のmongo集約クエリは、このようなものです:この種のmongo集約条件を春に書く方法は?

matchCondition = Aggregation.match(Criteria.where("event_state") 
        .is("scheduled").and("schedule.end_time").gt(d) 
        .orOperator(Criteria.where("event_state").is("live"))); 

バリアント2:

matchCondition = Aggregation.match(Criteria 
        .where("event_state") 
        .is("live") 
        .orOperator(
          Criteria.where("event_state").is("scheduled") 
            .and("schedule.end_time").gt(d))); 

ソートや投影条件

db.events.aggregate({ 
    "$match" : { $or : [ 
      {"event_state" : "live"}, 
      { 
       $and: [ 
        {"event_state" : "scheduled"}, 
        {"schedule.start_time" : { "$gt" : ISODate("2016-12-15T14:06:00.000Z")}} 
        ] 
      } 
      ] 
     } }, 
     { "$sort" : { "schedule.start_time" : 1}} , 
     { "$project" : { 
      "registered_users_count" : { "$size" : [ "$registered_users"]} , 
      "event_image" : 1 , "celebrity" : 1 , "name" : 1 , "category" : 1 , 
      "schedule" : 1 , "online_moderator" : 1 , "offline_moderator" : 1 , 
      "region" : 1 , "status" : 1 , "event_state" : 1 , "recorder_id" : 1 , 
      "webcast_url" : 1 , "replay_url" : 1 
     }}) 

は私が1バリアント

以下のようなものを試してみました

sortCondition = Aggregation.sort(Sort.Direction.ASC, 
        "schedule.start_time"); 

AggregationOperation projectValues = Aggregation.project() 
       .and("registered_users").size().as("registered_users_count") 
       .and("event_image").as("event_image").and("celebrity") 
       .as("celebrity").and("name").as("name").and("category") 
       .as("category").and("schedule").as("schedule") 
       .and("online_moderator").as("online_moderator") 
       .and("offline_moderator").as("offline_moderator").and("region") 
       .as("region").and("status").as("status").and("event_state") 
       .as("event_state").and("recorder_id").as("recorder_id") 
       .and("webcast_url").as("webcast_url").and("replay_url") 
       .as("replay_url"); 

Aggregation aggrigation = Aggregation.newAggregation(matchCondition, 
       sortCondition, projectValues); 

変異体1および変異体2のいずれも所望の状態を生成していない。だからこれを達成する方法は?

+0

はこれを試してみてください。 OrOperatorが最初に来なければなりません。 Aggregation.match(Criteria.where( "event_state") .is( "scheduled")。( "schedule.start_time")。gt(d)、Criteria.where( "event_state")。 ( "live"))) – Lipu

+0

"Criteria.orOperator"は上記のように機能しません。基準の後には、「どこで」または「クラス」と書くことができるからです。私が同じことを書こうとした場合のイベント。それは私に次のように表示されます:非基準メソッドorOperator(Criteria ...)への静的参照をタイプCriteriaから作成できません。 – Kiran

+0

申し訳ありません。 'new' operator.Aggregation.match(新しいCriteria().Operator(criteria1、criteria2))を使用してください。 – Lipu

答えて

1

spring-data-mongo API documentationを作成してください。Criteria.OrOperatorsは、複数のCriteriaを引数として取って、以下のようなaまたはoperationを作成します。 次のようにこれに対する解決策は次のようになります。

Aggregation.match(new Criteria().orOperator(Criteria.where("event_‌​state") .is("scheduled").and("schedule.start_time").gt(d), Criteria.where("event_state").is("live"))) 
関連する問題