Ticket:Projection

@Nu_81142, what error do you get?

server@1.0.0 test C:\Nu_MongoDB
jest --passWithNoTests “projection”

Determining test suites to run…Setup Mongo Connection
FAIL test/projection.test.js
Projection
× Can perform a country search for one country (49ms)
× Can perform a country search for three countries (11ms)

● Projection › Can perform a country search for one country

expect(received).toEqual(expected)

Expected value to equal:
  2
Received:
  0

   9 |     const kosovoList = ["Kosovo"]
  10 |     const movies = await MoviesDAO.getMoviesByCountry(kosovoList)
> 11 |     expect(movies.length).toEqual(2)
     |                           ^
  12 |   })
  13 |
  14 |   test("Can perform a country search for three countries", async () => {

  at toEqual (test/projection.test.js:11:27)
  at tryCatch (node_modules/regenerator-runtime/runtime.js:62:40)
  at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:296:22)
  at Generator.prototype.(anonymous function) [as next] (node_modules/regenerator-runtime/runtime.js:114:21)
  at step (node_modules/babel-runtime/helpers/asyncToGenerator.js:17:30)
  at node_modules/babel-runtime/helpers/asyncToGenerator.js:28:13

● Projection › Can perform a country search for three countries

expect(received).toEqual(expected)

Expected value to equal:
  2788
Received:
  0

  15 |     const countriesList = ["Russia", "Japan", "Mexico"]
  16 |     const movies = await MoviesDAO.getMoviesByCountry(countriesList)
> 17 |     expect(movies.length).toEqual(2788)
     |                           ^
  18 |     movies.map(movie => {
  19 |       const movieKeys = Object.keys(movie).sort()
  20 |       const expectedKeys = ["_id", "title"]

  at toEqual (test/projection.test.js:17:27)
  at tryCatch (node_modules/regenerator-runtime/runtime.js:62:40)
  at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:296:22)
  at Generator.prototype.(anonymous function) [as next] (node_modules/regenerator-runtime/runtime.js:114:21)
  at step (node_modules/babel-runtime/helpers/asyncToGenerator.js:17:30)
  at node_modules/babel-runtime/helpers/asyncToGenerator.js:28:13

console.error src/dao/moviesDAO.js:74
Unable to issue find command, Error: expect(received).toBe(expected) // Object.is equality

Expected: 2
Received: 17

console.error src/dao/moviesDAO.js:74
Unable to issue find command, Error: expect(received).toBe(expected) // Object.is equality

Expected: 2
Received: 17

Test Suites: 1 failed, 1 total
Tests: 2 failed, 2 total
Snapshots: 0 total
Time: 3.097s
Ran all test suites matching /projection/i.
Teardown Mongo Connection
npm ERR! Test failed. See above for more details.

I am not sure that this is how you specify the projection using the syntax you use. I prefer the syntax db.collection.find( {…} ).project( {…} ). You do not need _id:1 in your projection as _id is always returned unless explicitly excluded. But I don’t think it cause any harm.

I updated my code the way you recommended and still failed.

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.
  // cursor = await movies.find({country{$in:countries}},{title:1}).limit(1)
  cursor = await movies.find(
			{countries:{$in:countries}}).project({title:1})				
			
  expect(cursor).not.toBeNull()
  expect(Object.keys(cursor).length).toBe(2)
} catch (e) {
  console.error(`Unable to issue find command, ${e}`)
  return []
}

return cursor.toArray()

}

Remove square brackets from countries and it will work
{countries:{$in:countries}} and do not change the rest of the code

2 Likes

Thank you! It works!

Why would i know this? how can use the api. SMH. this is frustrating

I used this code…

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

My question is… A. How can i figure out the api documentation that would inform me that I can use .project()…
B. Also, how would I know that the countries shouldn’t be incased in representing an array?
C. How can I print out my value like i can in the shell? This for 100

thanks so much

Any particular reason, why?

thanx @Marcio_54378 sir, you saved my course here . . .

Yo llegue tarde a esta respuesta :sob:

Use “$in”
No need of cursor var

I set my timeout to 300000 and I succeed!

I do not see the square brackets you are talking about

Hi @Nicholas_72039,

Please share the pipeline you are getting error with.

Kanika

Thank you for reaching out. I was able to make it work by removing some unnecessary code.

1 Like

How? i get the same error as you, here’s my code:

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.
      cursor = await movies.find(
        {countries: {$in: countries}},
        {projection: {title: 1}}
      ).limit(1)
    } catch (e) {
      console.error(`Unable to issue find command, ${e}`)
      return []
    }

    return cursor.toArray()
  }

I still get the same error.

Hi @learner1283,

Please try after removing the limit operator.

Kanika