Chapter 3 -> Ticket: User Report

Guys, I’m not getting sorting the top 20 commenter in my pipeline.
I’ve tried in Compass first, so I wrote my $lookup, then $unwind to transform the mostActive Array in Object and then $sort, but in this one I receive the following error:
Error in $cursor stage :: caused by :: operation exceeded time limit

Anyone can hep me fixing this issue?

Hi @brunomathidios,

Are you sure about the $unwind part ? Also, think about ways to limit your result set.

You are getting this on Compass correct?
Compass set’s a $maxTimeMS by default to prevent overloading the server with exploratory workloads.

In any case, I would consider a different approach to using $lookup or $unwind, there’s a simpler way to implement this ticket!

N.

1 Like

I’m trying to use $lookup with $group and then $sort.
If you can give more ideas, I’d be happy :slight_smile:

Hi @brunomathidios
You can try to build a aggregation pipeline with the stages “group - sort - limit”…

Hello @fsvargas, thanks for your attention.
If I understood correctly, I should implement this pipeline in the comments collection, correct?
Because I’ve done that and until now the first document retrieved does not contain the field count with the value 909…

Guys, I finished this ticket!
Using aggregation through Compass, I wasn’t getting the correct values and doing that on mongo shell, yes!
It’s weird I now, but my unit test passed and then my validation code too.

Thank you for your attention

Congrats… @brunomathidios

Same here - Compass didn’t show the same results as the same aggregation pipeline in the shell or via Java driver and Java version of the same pipeline…yes, weird…there is still some woodoo out there…

Congrats! I passed this ticket too.

1 Like

Hi @brunomathidios @fsvargas ! I have little problem - can’t understand how can we get the list of Critic ? when I implement this pipeline in shell - I get the expected result. But why and how we should get the list of Critics? Thats a point what stop me from passing this ticket!

Hi @jekafortuna, I think there are some ways to complete this task. Your pipeline should retrive only properties of the Critic object.
I could retrieve the result using AggregateIterable iterable.
I mounted my pipeline and with the result, I used the into method from iterable variable to convert in a list of Document. Then I iterated through it and added the properties of the Critic object in the mostActive list.

Sorry for the short answer, but I believe you can go on with it!!!

2 Likes

Hi @jekafortuna - @brunomathidios
You have a pipeline object ( List<Bson> ) with the respective stages (group , sort, limit).

From the documentation for Agreggate Method…
https://mongodb.github.io/mongo-java-driver/3.9/javadoc/com/mongodb/client/MongoCollection.html

You can use the aggregate method with the next parameters:
aggregate​(List<? extends Bson> pipeline)
or
aggregate​(List<? extends Bson> pipeline, Class resultClass)

I hope this can help you…

7 Likes

Thank you a lot guys @brunomathidios @fsvargas for help!

That works! Thank you!

Hmmm, I am still a bit confused as tyo how we’re supposed to set up the class. Should it look like the below?

mostActive = commentCollection.aggregate(pipeline, ArrayList.class).iterator().tryNext();

Hi @Nate_93535,

You don’t need to use iterator here. You need to return the whole list at once.

Kanika


I’ve tried a number of different combinations, but it won’t conform to the list I need.

Please try after removing the second parameter, it is not necessary.

Kanika