【发布时间】:2021-01-13 01:33:51
【问题描述】:
这不是问题
我在spring boot聚合中将objectId转换为字符串时遇到了很长时间的问题,我找不到任何有用的方法来解决它。 最后,我想通了,我想把我的方法分享给有同样问题的人; 如您所知,查找需要查找相同值的两侧,例如,两侧 objectId 或两侧字符串 如果您在另一侧有 objecteId 和 string,则必须将此 objectId 设置为字符串,然后编写查找阶段; 查找之前的阶段应该是使用 $toString 表达式的项目阶段,如下所示:
ProjectionOperation projectionOperation = Aggregation.project(/*your nedded fields */)
.and(ConvertOperators.ToString.toString("$_id)).as("aggId");
然后你可以像下面这样轻松地使用查找:
Aggregation agg = Aggregation.newAggregation(
Aggregation.match(
Criteria.where("cratorId").is(userId)
)
,
projectionOperation
,
Aggregation.lookup("post", "aggId", "courseId", "postList"),
我的完整汇总是:
ProjectionOperation projectionOperation = Aggregation.project(/*your nedded fields */)
.and(ConvertOperators.ToString.toString("$_id")).as("aggId");
Aggregation agg = Aggregation.newAggregation(
Aggregation.match(
Criteria.where("creatorId").is(userId)
)
,
projectionOperation
,
Aggregation.lookup("post", "aggId", "courseId", "postList")
);
return this.aggregate(agg, entityClass, Object.class).getMappedResults();
希望对你有用
【问题讨论】:
标签: java mongodb spring-boot mongodb-query mongotemplate