Ticket - getComment

Bson match = Aggregates.match(eq("_id", new ObjectId(movieId)));

// TODO> Ticket: Get Comments - implement the lookup stage that allows the comments to
// retrieved with Movies.


// pipeline.add(unwind("$comments",new unwindOptions().preserveNullAndEmptyArrays(false)));

Bson groupCommentsSet = Aggregates.group("$comments", Accumulators.push("comments","$comments"));

Document movie = moviesCollection.aggregate(pipeline).first();

I have also tried to create it in compass:

List joinFilters =Arrays.asList(new Document("lookup", new Document("from", "comments") .append("let", new Document("id", "_id"))
.append(“pipeline”, Arrays.asList(new Document("$match",
new Document("$expr",
new Document("$and", Arrays.asList(new Document("$eq", Arrays.asList(movieId, “$$id”)),
new Document("$eq", Arrays.asList("$movie_id", “$$id”))))))))
.append(“as”, “comments”)),
new Document("$unwind",
new Document(“path”, “$comments”)
.append(“preserveNullAndEmptyArrays”, false)));

but the compass query doesn’t work.
The pipeline works, I see the 147 results but they are not sorted. How do I sort this?

Hi @Poonam_83472,

You are going in the right direction here. Java uses Aggregates.lookup for $lookup stage. Please read the following documentation page for the reference:


Hint: Try to use Aggregates.lookup function here instead of adding it into pipeline.

Let me know if you need more help.


I don’t get what I’m missing here…

List pipeline = new ArrayList<>();
Bson match = Aggregates.match(Filters.eq("_id", new ObjectId(movieId)));
List<Variable> let = Arrays.asList(new Variable(“id”, “$_id”));
Bson joinedMatch = Aggregates.match(Filters.expr(Filters.eq(“movie_id”, “$$id”)));
Bson joinedSort = Aggregates.sort(Sorts.ascending(“name”));
List joinedPipeline = Arrays.asList(joinedMatch, joinedSort);
Bson lookup = Aggregates.lookup(“comments”, let, joinedPipeline, “comments”);
Document movie = moviesCollection.aggregate(pipeline).first();

I get this error: GetCommentsTest.testGetMovieComments:71 Comments list size does not match expected expected:<147> but was:<50329>

In the lessons , you only have adding each stage to the pipeline. Not sure what you mean. Moreover, can you please point me to examples not class docs. I am struggling with grouping after unwind in compass.

I tried this in compass

from: ‘comments’,
let: {id: ‘$_id’ },
{ ‘$expr’ :

    {'$eq' : ['$movie_id', '$$id']}

{ ‘$unwind’:{
path: ‘$comments’,
preserveNullAndEmptyArrays: false}
{ ‘$sort’ : {“date”: -1 }}

as: ‘comments’

But, even though I have unwind it gets comments without any data in it.

I have taken another shot at it:
List variables = Arrays.asList(new Variable<>(“id”, “$_id”));
List pp = Arrays.asList(new Document(“match”,
new Document(“expr”,
new Document("$eq", Arrays.asList("$movie_id", “$$id”))))
.append(“as”, “comments”));
Bson lookup = Aggregates.lookup(“comments”,variables, pp,“comments”);


Document movie = moviesCollection.aggregate(pipeline).first();

But, I get error:
com.mongodb.MongoCommandException: Command failed with error 40323 (Location40323): ‘A pipeline stage specification object must contain exactly one field.’ on server cluster0-shard-00-02-hhrit.mongodb.net:27017. The full response is { “operationTime” : { “$timestamp” : { “t” : 1569957980, “i” : 1 } }, “ok” : 0.0, “errmsg” : “A pipeline stage specification object must contain exactly one field.”, “code” : 40323, “codeName” : “Location40323”, “$clusterTime” : { “clusterTime” : { “$timestamp” : { “t” : 1569957980, “i” : 1 } }, “signature” : { “hash” : { “$binary” : “pSGJk0NWGny56xqsvyJl5jf0uj8=”, “$type” : “00” }, “keyId” : { “$numberLong” : “6738349885474471937” } } } }

This is how it shld be:
List pp = Arrays.asList(match(expr(new Document("$eq", Arrays.asList("$movie_id", “$$id”)))),
sort(new Document(“date”, -1L)) );

Wish somebody would have helped earlier:
Found this reference:

1 Like

Hi @Poonam_83472, @Shakira,

Please look at the response here: