Ticket: Project is returning 17 instead of 2

Here is my code.

 cursor = await movies.find({countries:{$in: countries}}).project({ title: 1, id: 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()
  }

My error:

Determining test suites to run...Setup Mongo Connection
 FAIL  test/projection.test.js
  Projection
    ✕ Can perform a country search for one country (20ms)
    ✕ Can perform a country search for three countries (5ms)

  ● 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:69
    Unable to issue find command, Error: expect(received).toBe(expected) // Object.is equality
    
    Expected: 2
    Received: 17

  console.error src/dao/moviesDAO.js:69
    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:        1.289s, estimated 2s

I solved it like this…

let cursor
try {
let search= {countries:{$in: countries}} //{countries:{all:{countries}}} let projection = {title:1,_id:1} cursor = await movies.find(search).project(projection) } catch (e) { console.error(`Unable to issue find command, {e}`)
return
}

return cursor.toArray()

}

A little confused by the let search as well as the two // before {countries:{all:countries}}}

let search= {countries:{$in: countries}}
let projection = {title:1,_id:1}
cursor = await movies.find(search).project(projection)

or

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

For some reason this didn’t fix it. Trying to figure out what has gone wrong.

for me these lines must be eliminated

expect(cursor).not.toBeNull()
expect(Object.keys(cursor).length).toBe(2)

1 Like

Thank you. That worked!

Never change unit test code. You must fix your code.

2 Likes

find(query, options)

One of the options is projection (optional)

The fields to return in the query. Object of fields to include or exclude (not both), {‘a’:1}

For example, get all movies ({}) & limit the return data to title & year and exclude _id

Compass

Node driver

cursor = await movies.find( {}, {projection: { title: 1, year: 1, _id: 0} )

The error:

In your case you missing the projection option (So you returning 17 fields instead of two title and id).

should return (-or- {title: 1})

1 Like