如何在Spring中编写这种mongo聚合匹配条件?

如何在Spring中编写这种mongo聚合匹配条件?,spring,spring-mvc,mongotemplate,Spring,Spring Mvc,Mongotemplate,我的mongo聚合查询如下所示: db.events.aggregate({ "$match" : { $or : [ {"event_state" : "live"}, { $and: [ {"event_state" : "scheduled"}, {"schedule.start_time" : { "$gt" :

我的mongo聚合查询如下所示:

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:

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)));
排序和投影条件

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均未产生所需条件。那么如何实现这一点呢?

形成spring数据mongo标准。OrOperator将多个标准作为参数,形成一个或类似的操作。 因此,解决方案如下:

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

试试这个。操作员必须是第一位的。聚合.match(标准.orOperator(标准.其中(“事件状态”)为(“计划”)和(“计划.开始时间”).gt(d),标准.其中(“事件状态”)为(“活动”))“标准.orOperator”未按上述方式工作。因为在标准之后,您可以写“where”或“class”。事件,如果我试图写相同的。它向我显示了以下内容“无法从类型条件中静态引用非静态方法或运算符(条件…)”。对此表示抱歉。使用“new”操作符.Aggregation.match(new Criteria().orOperator(criteria1,criteria2))非常感谢@Lipu。请把你的建议作为答案。所以我可以接受。