Ticket: Get Comments - sorting comments

I have a question regarding the detailed answer you propose for this ticket.
I will not publish the answer, but I don’t understand how your suggested solution implements sorting the comments by date, descending?

Hi @Irena_Zaidman1,

You are right. Thanks for sharing this and we will figure and fix this out.

cc @Sonali_Mamgain


Spent a lot of time on the sorting of the comments.:slight_smile:

Hi @kanikasingla
Any hint or help on sorting the comments?

Honestly, I don’t know where I could place that Sort because it’s a sort on the foreign collection.
I have been trying to find any example but I’ve found mostly on StackOverflow with samples which not use MongoDB Driver helper methods.
Any help or documentation (with some samples) would be very much appreciated.
I don’t know if the AggregateLookupOptions on the Lookup method could help on this.
I’ve invested a lot of time on this and I don’t see the light yet.

What is the error when you are not adding the sort operator?


There is no error at all, as I’m not being able to apply any “Sort”.
The same happens (no Sort) with the Detailed Answer provided for that Ticket: Get Comments (Chapter 3)

         return await _moviesCollection.Aggregate()
             .Match(Builders<Movie>.Filter.Eq(x => x.Id, movieId))
                 m => m.Id,
                 c => c.MovieId,
                 (Movie m) => m.Comments

My question is the same as the original post is: how/where to apply the “Sort” as part of the requirements from the ticket is:
“The comments should be returned in order from most recent to least recent using the date key.”

PS: Apologies for putting the answer (partially) but I needed to be explicit as there’s no error but unfulfillment of requirements.


Hi @Sebastian_Inones,

You are right, don’t worry about the sort. Are you able to pass integration test?

If yes, let us know and we can fix this from our backend :slight_smile:


Yes. I already passed the Integration Tests!
On the other hand, wouldn’t help if the Solution Code is hosted in some Git Repository for instance and we could make also comments there and/or Pull Request when we found some issues?
Sebastian Inones

1 Like

I don’t think that is possible at the moment. :slight_smile: But we have noted this down and will be fixing on our end. cc @Sonali_Mamgain


Hi all,

Thanks for reporting this.

I will share this with the team and get it updated on our end.

Please feel free to reach out if you have any questions.


So, extending the answer, is it possible to sort the comments by some field in the “sub-query” (lookup method)?
In the case of yes, how can I do it?
Thank you very much.

Using the expressive lookup. And pipeline operator :slight_smile:

Thank you kanikasingla.
I´m a beginner so I´m a little bit lost. Could you give me an example (perhaps, sort by name desc) over Sebastian code´s.
Thank you very much.

Hi @DavidCon,

Actually, can you try running the test without sort? Unit test and integration test both :slight_smile:


This doesn’t work. The only way I managed to return the requested result was to sort the comments afterwards using LINQ, so on the client side.

I would really like to know if there is a solution to this problem, using the C# driver.

1 Like


I’m unable to achieve the Sort of the comments in the pipeline too.

The current detailed answer doesn’t sort anything yet. Could you please give us the full answer ?

(Is it possible to do this sort with lambda expression ?)

Hi @JohnJohn,

Please skip sorting the comments for now and let me know if you have any issue in getting the validation code.


Hi @kanikasingla,

I don’t have any issue with the validation code.
But as I’m here to learn, I would like to know the full answer, especially since I’m currently not able to figure a way to do it myself.


Having the same issue

I’m having the same issue too. I’m not able to insert this sort definition in the lookup() method:
Builders<Comment>.Sort.Descending(c => c.Date);