M220N Chapter 2

Hello everyone. I’m on chapter 2. The projection. Did I miss a section on how this is supposed to be done? In SQL I could easily "Select m.movies where m.Countries = “whatever” … I’ve added deleted so much code just trying to get this one thing started, I have nothing worthwhile. It’s sort of my last attempt before I go back to SQL.

I have a collection from the movies collection
Here

var movieCollection = db.GetCollection<Movie>("movies");

I’m assuming I create a filter from this collection to find the countries, but I get errors
Because Countries is already a list in the model.
I’ve done this but it comes back empty

List<string> usa = new List<string>();
usa.Add("USA");

var betterFilter = Builders<Movie>.Filter.Eq(m => m.Countries, usa); //errors out not having a list param here

var movieList = await movieCollection.Find<Movie>(betterFilter).ToListAsync();

All just to get a list of countries based on the user selection. I am temporarily choosing USA cause it’s right in the first few documents.

Is there anyone out there who can give me just a little help understanding how to work this? Thanks

Hi @Louis_Degel, welcome to the community.
Can you please tell me the name of the function that you are modifying?
Ideally, it should be the GetMoviesByCountryAsync function as below:

public async Task<IReadOnlyList<MovieByCountryProjection>> GetMoviesByCountryAsync(
    CancellationToken cancellationToken = default,
    params string[] countries)
 {
    return await _moviesCollection
              .Find(m => m.Countries.Any(c => countries.Contains(c)))
              .SortByDescending(m=>m.Title)
              .Project<MovieByCountryProjection>(Builders<Movie>.Projection.Include(x => x.Title).Include(x => x.Id))
              .ToListAsync(cancellationToken);
}

In case you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

Hello, I would like to ask you what method of Builders.Filter we would use in this case if the filter is assigned to a filter variable, declared before the return. Thank you.