gpt4 book ai didi

java - 如何在Java Spring的mongoTemplate聚合中实现$SetIsSubset?

转载 作者:行者123 更新时间:2023-11-30 02:42:57 26 4
gpt4 key购买 nike

我正在尝试在 mongoTemplate 聚合类中使用 $setIsSubset 。但找不到任何解决办法。

我的 Mongo 查询是:

db.events.aggregate([
{ $match: { $and: [
{ "event_state" : {$in : ["live","scheduled"]} },
{ "schedule.end_time" : {$gt : ISODate("2016-12-12T06:30:00.000Z")} }
]
}
},
{$project:
{
"name" :1,
"category" :1,
"schedule":1,
"celebrity" :1,
"online_moderator" :1,
"offline_moderator" :1,
"region" :1,
"status" :1,
"event_state" :1,
"recorder_id" :1,
"webcast_url":1,
"replay_url":1,
"registered_users":1,
registeredUsers: { $size:"$registered_users" },
is_registered:
{ $setIsSubset: [[ObjectId("584e6253e17ed10f0a8cba1d"),ObjectId("583e9719e17e8c1bf80da2fe")], "$registered_users.user_id"]}
}
},
{ $sort : {
"schedule.start_time": 1
}
}
]);

我已经转换了大部分代码,如下所示。但对 $setIsSubset 条件感到震惊。

matchCondition = Aggregation.match(Criteria.where("event_state")
.in(listEventStates).and("schedule.end_time").gt(d));

AggregationOperation projectValues = Aggregation.project()
.and("registered_users").size().as("registered_users_count")
.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");

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

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

编辑:

根据@user_531的请求。我正在用对我有用的可能解决方案来更新我的问题。我不知道它是否有效,因为我已经使用该解决方案两年了。之后我又改了几次代码,不再维护这个项目了。但值得一试。

AggregationOperation projectValues = Aggregation.project().and("registered_users").size()
.as("registered_users_count").and("event_image").as("event_image")
.and("name").as("name").and("category").as("category").and("schedule").as("schedule")
.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").and("registered_users").as("registered_users").and("room_details").as("room_details")
.and("live_stream_url").as("live_stream_url")
.and(new AggregationExpression() {
public DBObject toDbObject(AggregationOperationContext context) {
return new BasicDBObject("$setIsSubset", Arrays.<Object> asList(
Arrays.<Object> asList(new ObjectId("58528314e17edbb252692815")),
"$registered_users.user_id"));
}
}).as("is_registered");

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

AggregationResults<Events> results = mongoTemplate.aggregate(aggrigation, "events", Events.class);

最佳答案

利用AggressionExpression。在项目中包含以下投影。使用1.8.5 spring mongo数据版本。

and(new AggregationExpression() {
@Override
public DBObject toDbObject(AggregationOperationContext context) {
return new BasicDBObject("$setIsSubset",
Arrays.<Object> asList(
Arrays.<Object> asList("584e6253e17ed10f0a8cba1d",
"583e9719e17e8c1bf80da2fe"),
"$registered_users.user_id"));
}
}).as("is_registered"));

您可以将表达式包装为 Java 8 的 lambda

and(context -> new BasicDBObject("$setIsSubset",
Arrays.<Object> asList(
Arrays.<Object> asList("584e6253e17ed10f0a8cba1d",
"583e9719e17e8c1bf80da2fe"),
"$registered_users.user_id")))
.as("is_registered"));

关于java - 如何在Java Spring的mongoTemplate聚合中实现$SetIsSubset?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41117496/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com