M220 Chapter 2 Ticket: Projection

I am trying to complete this ticket, but I need some clarification. Do I need to define a filter and then the projection filter?

I defined the following:

var filter = Builders.Filter.In(m => m.Countries, countires);

I have switched between In and Eq, but the m => m.Countries errors out with the alert

Screenshot 2020-09-08 at 11.10.43 AM|690x332

The way I understand this to work is I have to first define the filter, then use the projection filter. Is this correct? Thanks.

Hi. I think the correct way of define filter is:

var fitler = Builders.Filter.In(“countries”, countries);

And is no matter when you define filter and projection filter. You can define filter and projection filter like a variables and inject their into return function OR define their directly in return collection.

I found another way to define it… But I haven’t tested it out yet.

I used the code:

var filter = Builders.Filter.AnyIn(m => m.Countries, countries);

That compares the contents of the array countries and matches it with anything in the Movie.Country field.

Ok… I have a queston… I am looking at the ticket for Chapter 2 where I need to build a filter that allows for a person to search the movie repository based on a comma separated search.

I used the following code.

My question is that the filter I defined should search the Movies.Country field and match the countries in the country array.

My projection filter includes Movie.Title with Movie.ID being included by default.

The find method calls the find helper and passes it the filter var which should be the records matched in the filter definition.

The sort sorts the entries by title.

I believe the rest is good, but when I run this I get the following error from my terminal. (I used a terminal from windows not the Visual Studio app)

Any Ideas?

In addition, why are there Assert.AreEqual statements in the lessons, but if we try and put them into our code, they do not exist in the current context?

Update:
I have done as much researching as I could about my problem and I cannot find anything on it. I am wondering what I need to do to deal with the object error. (It errors out on line 122 of the test). I have watched the lesson over 5 times to see if I have missed anything but I am not sure if I have.

My closest guess is that I need to change my filter to be a new BsonDocument filter. I would love to get some hints as to what I am doing wrong.

Hi @David_Thompson,

I have sent you a message to share the code so that we can debug this quickly. :slight_smile:

Kanika

This is the code that I entered for the ticket. I have changed the
original filter to the below one, but I still get the same error. I didn’t
see any reference that I needed to set the input as an object.

var filter = Builders.Filter.In(“countries”, countries);

        //var filter = Builders<Movie>.Filter.Text(countries);

        var projectionFilter = Builders<Movie>.Projection
            .Include(m => m.Title)
            .Include(m => m.Id); (I inserted this knowing that the

field is included by default. Don’t know if that makes a difference)

        var movieProjected = await _moviesCollection
            .Find<Movie>(filter)
            .SortByDescending(m => m.Title)
            .Project<Movie>(projectionFilter)
            .ToListAsync(cancellationToken);

I run the test from the test explorer window at the bottom of the visual
studio screen and I get the following error.

Test Name: TestGetMoviesByCountry
Test FullName:
M220NTests.M220NTests.MoviesRepositoryTests.TestGetMoviesByCountry
Test Source: C:\Users\David
Thompson\source\repos\mflix-cs\M220NTests\MoviesRepositoryTests.cs : line
118
Test Outcome: Failed
Test Duration: 0:00:00

Test Name: TestGetMoviesByCountry
Test Outcome: Failed
Result StackTrace:
at M220NTests.MoviesRepositoryTests.TestGetMoviesByCountry() in
C:\Users\David
Thompson\source\repos\mflix-cs\M220NTests\MoviesRepositoryTests.cs:line 122
at
NUnit.Framework.Internal.TaskAwaitAdapter.GenericAdapter1.BlockUntilCompleted() at NUnit.Framework.Internal.MessagePumpStrategy.NoMessagePumpStrategy.WaitForCompletion(AwaitAdapter awaitable) at NUnit.Framework.Internal.AsyncToSyncAdapter.Await(Func1 invoke)
at
NUnit.Framework.Internal.Commands.TestMethodCommand.RunTestMethod(TestExecutionContext
context)
at
NUnit.Framework.Internal.Commands.TestMethodCommand.Execute(TestExecutionContext
context)
at
NUnit.Framework.Internal.Commands.BeforeAndAfterTestCommand.<>c__DisplayClass1_0.b__0()
at
NUnit.Framework.Internal.Commands.BeforeAndAfterTestCommand.RunTestMethodInThreadAbortSafeZone(TestExecutionContext
context, Action action)
Result Message: System.NullReferenceException :
Object reference not set to an instance of an object.

I noticed in the video that the first filter sets the var to a new
BsonDocument. But the second improved filter doesn’t use this method. Is
this where I am going wrong? On my reading, the builders helper is a lot
like a constructor. (if I read it right. LOL)

Attached is a full screenshot of my IDE so you can understand where I am in
the program.

Any suggestions would be really helpful.

Respectfully,

David Thompson
Phone: +65 9456 1249
Email: dave@asiantradecentre.org

Is there any update on my problem?

Did you get my code? I responded to the email.

Hi @David_Thompson,

I replied in the messages, please check.

Kanika

Thanks, But I would like to ask that you look at your response and then look at the videos. There is a REAL difference between what you wrote and how the lectures tell you to process the query.

The lectures step you through the simple to the complex version of the query. However, your method provided to me that passes the test are WAY different. Is this something that I would learn in M121. (how to make a query reference an object?)

I didn’t take the courses in order because I thought that I would be stepped through how to do the labs in the course material. Should I unregister from this course and take the M121 course before taking this one? I have knowledge of SQL query language, so I thought I could piece it together.

What is your recommendation?

The course videos step you through setting the filter, projecting the filter, then outputting the filter. That is completely different from the return await command that you sent for the help.

We have recently launched a course which would be perfect to begin your journey with MongoDB. It can help you map sql knowledge to mongodb terms and concepts.

1 Like

Cc @Sonali_Mamgain

Hi @David_Thompson,

I have suggested few corrections in your code. Please check your discourse inbox and feel free to reply there if you have any questions.

Also, I agree with @kanikasingla on taking M100: MongoDB for SQL Pros course if you have prior experience with SQL .

We generally recommend taking M121: The MongoDb Aggregation course before diving into M220s.
Having prior knowledge of building aggregation pipelines would be beneficial for this course. You can share your familiarity with C# in my discourse message and I can help you with it as well.

I hope it helps!!

Let me know if you have any questions.

Thanks,
Sonali

1 Like

I’ve been stuck at the same spot for several hours. Please help

Hi @Erik_N_A,

I have sent a message to help on the same.

Kanika

@kanikasingla
What was the solution you sent to help Erik_N_A? I’m having the same problem it looks like he is with the type casting of the result.
Thanks

To follow up, I figured it out, but I feel like I have a much more complicated solution than should be necessary

1 Like

It happens @Beth_Lyman! :slight_smile: I am glad you were able to resolve it through. Good luck for the rest of the course.

Kanika

hi,
in my opinion this thread is not much helpful, because the answers are just in personal messages. :frowning:
Also I like to acknowledge David. For me the main problem was in the projection, which had to be done in some other way than the videos showed. For clarification:
if it is IMPORTANT to have:
.Project<MovieByCountryProjection>(projectionFilter)
it should have been explained before.

Nevertheless I really liked the courses so far.
Thank you. :slight_smile:

2 Likes

just try it :wink:

var filter = Builders<Movie>.Filter.In("countries", countries);
        var projectionFilter = Builders<Movie>.Projection.Include(x => x.Id).Include(x => x.Title);

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