0
我蒙戈聚集查詢是這樣的:如何在Spring中編寫這種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都沒有產生所需的條件。那麼如何實現這一目標呢?
試試這個。 OrOperator必須先來。 Aggregation.match(Criteria.orOperator(Criteria.where(「event_state」)) .is(「scheduled」)and(「schedule.start_time」)。gt(d),Criteria.where(「event_state」)。is (「live」))) – Lipu
「Criteria.orOperator」不像上面提到的那樣工作。因爲在標準之後你可以寫出「哪裏」或「班級」。事件,如果我試圖寫相同。它向我顯示了以下內容:「不能對類型Criteria的非靜態方法orOperator(Criteria ...)進行靜態引用」。 – Kiran
對不起。使用'new'operator.Aggregation.match(new Criteria()。orOperator(criteria1,criteria2)) – Lipu