Chapter 2: User-Facing Backend Ticket: Get Comments

Hi,

I’ve implemented lookup stage in the getMovie method as below.

Bson match = Aggregates.match(Filters.eq("_id", new ObjectId(movieId)));
pipeline.add(match);
Bson lookup = Aggregates.lookup(“comments”, Arrays.asList(new Variable<>(“mov_id”, “$_id”)),
Arrays.asList(
Aggregates.match(Filters.expr(Filters.eq(“movie_id”, “$$mov_id”)))),
“comments”);

pipeline.add(lookup);

System.out.println(pipeline);
Document movie = moviesCollection.aggregate(pipeline).first();

when I printed the pipeline in the console, it shows as below:
[Stage{name=’$match’, value=Filter{fieldName=’_id’, value=573a13b5f29313caabd42c2f}}, Stage{name=’lookup', from='comments', let=[Variable{name='mov_id', value=_id}], pipeline=[Stage{name=’$match’, value=Filter{fieldName=’$expr’, value=Filter{fieldName=‘movie_id’, value=$$mov_id}}}], as=‘comments’}]

This implementation is not working in Java. And when I use Filters.eq("$movie_id", “$$mov_id”), it’s error out with “$movie_id” is unrecognized.

I also tried changing the implementation by isolation the variables(let) and pipeline(aggregate) as per the below guide lines. Still not working.

Can someone please help me to get this solved…

Sriharsha Tumati

Hi @Sriharsha_72933,

It’s hard to understand the aggregation pipeline.
I would have opened Compass or Mongo shell and started working on ticket by building the aggregation pipeline and would have tested there first.

After that, you can then convert that pipeline to Java Aggregation class using “Aggregates.lookup” method.

Below threads might be helpful:

Kanika

1 Like