M220N User Report Ticket

Trying to figure out this User Report ticket. here is my current code:

     var filter = Builders<BsonDocument>.Projection
                     .Include("count")
                     .Exclude("_id");

                result = await _commentsCollection
                    .WithReadConcern(ReadConcern.Majority)
                    .Aggregate()
                    .Group(new BsonDocument
                        {
                            { "id", "$email" },
                            {"count",
                            new BsonDocument ("$sum", 1)}
                        })
                    .Sort(new BsonDocument("count", -1))
                    .Limit(20)
                    .Project<ReportProjection>(filter)
                    .ToListAsync();

I think I need to use a Bson document instead of a builder for the projection, but am not sure how to do that. Any guidance welcome!

I think you want to sort based on the number of comments rather than the email.

1 Like

Thanks. I have adjusted that but am still getting the following error:

Argument 1: cannot convert from ‘MongoDB.Driver.ProjectionDefinition<M220N.Models.Projections.ReportProjection>’ to ‘MongoDB.Driver.ProjectionDefinition<MongoDB.Bson.BsonDocument, M220N.Models.Projections.ReportProjection>’ [/Users/shelby/Downloads/mflix-cs/M220N/M220N.csproj]

This is too specific to .Net for me to help. I do not know .Net and I am not planning to learn.

May be id within Group needs to be _id instead. But that won’t fix the error.

gotcha. thanks anyway!

Figured it out. I needed to use in my builder for the projection instead of .

2 Likes