Projection not working

I’m trying with many different options, but i’m not getting the result and getting errors. please help.

cntrylist=list(db.movies.find({“countries”: {"$in": [“countries”]}},{ “title”: 1}))[0]

    #         return list({db.movies.find()})
    # return (cntrylist)
    #         return list({db.movies.find({"countries": {"$in": ["countries"]}},{ "title": 1})})[0]
    cntrycount = 0
    #         cntrylist = list(db.movies.find({"countries": {"$in": ["countries"]}},{ "title": 1}))[0]
    #         cntrylist = list(db.movies.find({"countries": {"$in": ["countries"]}},{ "title": 1}))
    
    #         cntrycount = list(db.movies.find({"countries": {"$in": ["countries"]}},{ "title": 1}))[
    #             0].get("count")
    #         return (cntrycount, 10)
    cntrycount = db.movies.find({"countries": {"$in": ["countries"]}},{ "title": 1}).count()
    #         return (cntrylist, cntrycount)
    #         return cntrylist
    #         return list({db.movies.find({"countries": {"$in": ["countries"]}},{ "title": 1})})[0], db.movies.find({"countries": {"$in":["countries"]}},{ "title": 1}).count()
    return (list({db.movies.find({"countries": {"$in": ["countries"]}},{ "title": 1})})[cntrycount], db.movies.find({"countries": {"$in":["countries"]}},{ "title": 1}).count())

return list({db.movies.find({“countries”: {"$in": [“countries”]}},{ “title”: 1})})[cntrycount]

    #         return list({db.movies.find({"countries": {"$in": ["countries"]}},{ "title": 1})}, {db.movies.find({"countries": {"$in":["countries"]}},{ "title": 1}).count()})  -- TypeError: object of type 'TypeError' has no len()
    #         return list({db.movies.find({"countries": {"$in": ["countries"]}},{ "title": 1})})

error detail:

  assert len(result) == 2

E TypeError: object of type ‘Cursor’ 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 ‘Cursor’ has no len()

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

C:\Users\prasa\Desktop\Mongodb related\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:\Users\prasa\Desktop\Mongodb related\m220\mflix-python, inifile:
plugins: remotedata-0.2.1, openfiles-0.3.0, flask-0.10.0, doctestplus-0.1.3, arraydiff-0.2
collected 39 items / 37 deselected

tests\test_projection.py .F [100%]

================================== FAILURES ===================================
_____________________ 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 assert 2 == 2421
E + where 2 = len((<pymongo.cursor.Cursor object at 0x000002C9FD7273C8>, 0))

tests\test_projection.py:22: AssertionError
============== 1 failed, 1 passed, 37 deselected in 5.29 seconds ==============

C:\Users\prasa\Desktop\Mongodb related\m220\mflix-python>

Hello prasanthsp84,

It looks like the error thrown during the unit test was this one:

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

Which would imply that get_movies_by_country() returns a Cursor. Read the description of get_movies_by_country() to figure out the correct return type!

Matt

Hello Matt

Thank you for your support!
I had used extra braces, brackets etc.

1 Like

Hi

Can you share where is the extra brackets ?

thanks

just use this at the end of your method and it will run:

#######
return list(db.movies.find({‘countries’:{’$in’:countries}},{‘title’:1}))
#######

notice that by default the “_id” field will be always showed unless you set to NOT show it.

Give a man a fish and you feed him fir a day, teach a man to fish and you feed him for the rest of the day.

You should explain him why ‘countries’ is different fron countries.

thank for your reply,
now its working
it should be

return list(db.movies.find({“countries”:{"$in":countries}},{“title”:1}))

although i tried to run it hundred of times without successfull
now it works.

1 Like

go ahead, fell free to do that :wink:

When you use ‘countries’ or “countries”, you have a string value. In the context above you want to refer to the parameter countries for the argument of $in (the second occurence of countries). That is why countries work and ‘countries’ does not. As for the first occurence of countries you do need to use ‘countries’ since it is the name of the field.