M220N Ticket Projection

Ok… I have to get use to the fact that the aggregation pipeline looks a LOT different in the .net environment than in the aggregations class.

I am struggling to get the jist of what is required in the Ticket Projection lab. Here is my thought process.

I created a variable for the filter using the builder and using the .All or searching the countries array and using the countries input.

I then created a projectionFilter using the builder .projection and included the title and ID.

This should have satisfied the $ match and $ project stages. right?

But in the return statement is where I am getting stuck. I included my filter in the .Find portion… I then sorted using .SortByDecenting on the title like the requirements stated (it states I need to sort before the project.

Next I used the projectFilter in the .projected portion.

Lastly I use ToListAsync and passed the cancellationTolken.

My problem comes when I try to use the projection filter… I get an error and I need to cast the results for a read only. If I do this, the test fails.

Thoughts or recommendations?

I have included my code for clarification and used the blur so it can only be seen by the right people.

var filter = Builders<Movie>.Filter.All("countires", countries);

           var projectFilter = Builders<Movie>.Projection
               .Include(m => m.Title)
               .Include(m => m.Id);


           return await _moviesCollection
              .Find(filter)
              .SortByDescending(m => m.Title)
              .Project(projectFilter)
              .ToListAsync(cancellationToken);

The system wants to change it to this:


            var filter = Builders<Movie>.Filter.All("countires", countries);

            var projectFilter = Builders<Movie>.Projection
                .Include(m => m.Title)
                .Include(m => m.Id);


            return (IReadOnlyList<MovieByCountryProjection>)await _moviesCollection
               .Find(filter)
               .SortByDescending(m => m.Title)
               .Project(projectFilter)
               .ToListAsync(cancellationToken);

Am I missing something here (obviously I am) LOL

Update… I got the test to run… But my filter seems to be not working… In the aggregation pipeline, there was a way to test each “stage” This seems to not be possible in this form.

I used the Builder to find.All which should take the first argument at the document field (in this case it is an array) and the second argument should take the param string array in countires.

The .all should match all entries in the param array of countries with the field array in the first argument…

I am correct in this right?

Builders<Movie>.Filter.All("countires", countries)

Update number 3… Got the query to run, but the test still fials.

It says expected: 1237 but was: 8.

This leads me to believe that my filter is off somehow. Again. I identified the filter:

var filter = Builders<Movie>.Filter.All("countries", countries);

I then passed it to the return await:

 return await _moviesCollection
               .Find(filter)
               .SortByDescending(m => m.Title)
               .Project<MovieByCountryProjection>(projectFilter)
               .ToListAsync(cancellationToken);

So… The query works, but it isn’t returning the right amount of documents… Thoughts or areas to point me in to refine my query?
Thanks

Update 4… Fixed it… It was in my query operator. I used the wrong array operator.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.