英文:
convert _id(ObjectId) to String in aggregation for lookup Spring boot
问题
我曾经长时间面临一个问题,就是在Spring Boot聚合中将objectId转换为字符串,而我找不到任何有用的方法来解决它。最终,我找到了解决方法,并愿意与那些有相同问题的人分享我的方法;
正如你所知道的,lookup需要两边的查找具有相同值,例如,两边都是objectId或两边都是字符串。
如果你一边有objectId,另一边有字符串,你需要将这个objectId转换为字符串,然后编写你的lookup阶段;
在lookup之前的阶段应该是使用$toString表达式的投影阶段,像下面这样:
```java
ProjectionOperation projectionOperation = Aggregation.project(/*你需要的字段 */)
.and(ConvertOperators.ToString.toString("$_id")).as("aggId");
然后你可以像下面这样轻松使用lookup:
Aggregation agg = Aggregation.newAggregation(
Aggregation.match(
Criteria.where("creatorId").is(userId)
)
,
projectionOperation
,
Aggregation.lookup("post", "aggId", "courseId", "postList"),
我的完整聚合代码如下:
ProjectionOperation projectionOperation = Aggregation.project(/*你需要的字段 */)
.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();
希望这对你有帮助。
<details>
<summary>英文:</summary>
**THIS IS NOT QUESTION**
I had a problem for a long time to convert objectId to string in spring boot aggregation and I can't find any useful way to solve it.
finally, I figure out and I like to share my way to those who have the same problem;
As you know lookup need two sides of lookup the same value, for example, two sides objectId or two sides string
if you have objecteId and string in another side you have to make this objectId to string and then write your lookup stage;
the stage before lookup should be the project stage with usage of $toString expression like bellow:
ProjectionOperation projectionOperation = Aggregation.project(/*your nedded fields */)
.and(ConvertOperators.ToString.toString("$_id)).as("aggId");
and then you can use lookup easily like below:
Aggregation agg = Aggregation.newAggregation(
Aggregation.match(
Criteria.where("cratorId").is(userId)
)
,
projectionOperation
,
Aggregation.lookup("post", "aggId", "courseId", "postList"),
my full aggregation is:
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();
hope it can be useful
</details>
# 答案1
**得分**: 1
我也在研究这个问题的答案,我找到了这个解决方案。
我正在使用 `spring-data-mongodb:3.0.0.RELESE`。
在您的聚合管道中:
```Kotlin
AddFieldsOperation.builder().addField("strId")
.withValue(
ConvertOperators.ToString.toString("$_id")
).build()
这相当于
{
$addFields: {
strId: {$toString: "$_id"}
}
}
您可以将 strId
替换为任何您想要的名称,并在接下来的操作中使用它。
英文:
I was looking into the answer to this question also and I found this solution.
I am using spring-data-mongodb:3.0.0.RELESE
.
In your aggregation pipeline
AddFieldsOperation.builder().addField("strId")
.withValue(
ConvertOperators.ToString.toString("$_id")
).build()
This is the equivalent to
{
$addFields: {
strId: {$toString: "$_id"}
}
}
You can replace strId
with any name you want and use it in the next operation.
答案2
得分: 1
List<Map> getMultiTable(){
ProjectionOperation projectionOperation = Aggregation.project(/*你所需的字段*/)
.and(ConvertOperators.ToString.toString("$_id")).as("_id")
.and(ConvertOperators.ToObjectId.toObjectId("$userId")).as("userId");
Aggregation agg = Aggregation.newAggregation(
Aggregation.match(
Criteria.where("status").is(0)
),
projectionOperation,
Aggregation.lookup("videoComment", "_id", "videoId", "commentList"),
Aggregation.lookup("user", "userId", "_id", "userList")
);
List<Map> result = mongoTemplate.aggregate(agg, "mediaFile", Map.class).getMappedResults();
System.out.println(result);
return result;
}
英文:
List<Map> getMultiTable(){
ProjectionOperation projectionOperation = Aggregation.project(/*your nedded fields */)
.and(ConvertOperators.ToString.toString("$_id")).as("_id")
.and(ConvertOperators.ToObjectId.toObjectId("$userId")).as("userId");
Aggregation agg = Aggregation.newAggregation(
Aggregation.match(
Criteria.where("status").is(0)
),
projectionOperation,
Aggregation.lookup("videoComment", "_id", "videoId", "commentList"),
Aggregation.lookup("user", "userId", "_id", "userList")
);
List<Map> result = mongoTemplate.aggregate(agg, "mediaFile", Map.class).getMappedResults();
System.out.println(result);
return result;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论