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
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
This is the equivalent to
You can replace
strId
with any name you want and use it in the next operation.