Error in projection Ticket

Hi guys.
I don’t understand what’s wrong with my code and I get the following error which I also don’t understand :

Any of you got an idea ?
Here is the little piece of code I wrote :

let result = await movies.findOne(
  {  countries: { $in: countries} },
  { projection : { title : 1}}
)

expect(result).not.toBeNull()

The test is expecting a list of movies. You might want to consider changing ‘findOne’ :slight_smile:

Kanika

Sure.
But I feel that the questions are very unclear about what is asked.
What variables do we use ?
What variable is returned ?
Or maybe it’s just me, but it is very unclear to me.
I find the course much easier than what is asked.
I’m French by the way so maybe I don’t understand everything that’s said.

Here is what I tried to write afterwards :

static async getMoviesByCountry(countries) {
    /**
    Ticket: Projection

    Write a query that matches movies with the countries in the "countries"
    list, but only returns the title and _id of each movie.

    Remember that in MongoDB, the $in operator can be used with a list to
    match one or more values of a specific field.
    */

    let cursor
    try {
      // TODO Ticket: Projection
      // Find movies matching the "countries" list, but only return the title
      // and _id. Do not put a limit in your own implementation, the limit
      // here is only included to avoid sending 46000 documents down the
      // wire.

      //===========================================
      
      //let filter = countries ;

      let results = await movies.find(
        { countries : { $in: countries }},
        { projection : { title : 1 }}
      );




      //===========================================
      cursor = await movies.find().limit(1)
    }
    catch (e) {
      console.error(`Unable to issue find command, ${e}`)
      return []
    }

    return cursor.toArray()
  }

I don’t understand what is expected to be returned.

Thanks a lot for your answer by the way

Same with this code. I understand the course, but I can’t think of a way to use it.

  //===========================================
  let countryFilter = countries ;

  let result = await movies.find(
    { countryFilter : { $in : countries }},
    { projection :  { title : 1 }},
  );
  
  cursor = await result.next()
  //===========================================

Sure. The Ticket: Projection expects the function: getMoviesByCountry to return a list of movies in the form of array.

If at any point in time, anything is unclear, you can go ahead and read the unit tests and see what it is expecting.

This code will get a list of movies from mongodb with filter and projection applied.

You don’t need this line of code now as you have already implemented the code above. Instead either you can change the variable name of results with cursor or assign results to cursor variable.

This line of code will send movies list in the form of array.

To help understand how the code works, what is being returned by the movies.find(..) try, putting console logs and see if this is the pattern that is expected by the unit test.

Kanika

Thank you very much. I guess I tried to do something too complicated.
I passed with like 2 lines of code.
Thanks again