Error running the Ticket: Projection. Solved

Hello, trying to run ticket: Projection I receive an error

(mflix) C:\MongoDB\M220\mflix-python>pytest -m projection
============================= test session starts =============================
platform win32 – Python 3.6.5, pytest-3.5.1, py-1.5.3, pluggy-0.6.0
rootdir: C:\MongoDB\M220\mflix-python, inifile:
plugins: remotedata-0.2.1, openfiles-0.3.0, flask-0.10.0, doctestplus-0.1.3, arr
aydiff-0.2
collected 39 items / 37 deselected

tests\test_projection.py FF [100%]

================================== FAILURES ===================================
________________________ test_basic_country_search_db _________________________

client = <FlaskClient <Flask ‘mflix.factory’>>

@pytest.mark.projection
def test_basic_country_search_db(client):
    countries = ['Kosovo']
    result = get_movies_by_country(countries)
  assert len(result) == 2

E TypeError: object of type ‘NameError’ has no len()

tests\test_projection.py:15: TypeError
_____________________ test_basic_country_search_shape_db ______________________

client = <FlaskClient <Flask ‘mflix.factory’>>

@pytest.mark.projection
def test_basic_country_search_shape_db(client):
    countries = ['Russia', 'Japan']
    result = get_movies_by_country(countries)
  assert len(result) == 2421

E TypeError: object of type ‘NameError’ has no len()

tests\test_projection.py:22: TypeError
=================== 2 failed, 37 deselected in 0.35 seconds ===================

Here is my part of db.py

def get_movies_by_country(countries):
“”"
Finds and returns movies by country.
Returns a list of dictionaries, each dictionary contains a title and an _id.
“”"
try:

    """
    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.
    """

    # TODO: Projection
    # Find movies matching the "countries" list, but only return the title
    # and _id.
    
    db.movies.find = {"countries": {"$in": filters["countries"]}}
    return list({db.movies.find()},{ "title": 1})

except Exception as e:
    return e

Please help if you can

Hello alroy,

There is no filters parameter passed to the get_movies_by_country method, so filters["countries"] is resulting in a NameError. The only parameter passed to this method is countries, which is an array of strings.

Your query predicate, {"countries": {"$in": filters["countries"]}}, is in the correct format, except filters does not exist.

Also, db.movies.find is not a variable, it’s a method! Which is to say, you shouldn’t assign anything to db.movies.find, you should call the method (db.movies.find()) and pass the predicate and projection to that method call.

I hope this helps!
Matt

Thank you, @mattjavaly

Solved

1 Like

Hi I am facing similar issue what was the problem?