Get_comments test failed with None results found

This is the assertion error I have:

>       assert len(result.get('comments', [])) == 126
E       AssertionError: assert 0 == 126
E        +  where 0 = len([])
E        +    where [] = <built-in method get of dict object at 0x7f7f8e00f360>('comments', [])
E        +      where <built-in method get of dict object at 0x7f7f8e00f360> = {}.get

And this is the pipeline I created:

Could you help me. I can not find information about how fix this issue. Thanks in advance.

Please post how you use the pipeline. The pipeline looks okay so it must be that you are not using it correctly.

movie = db.movies.aggregate(pipeline).next()
return movie

That looks fine.

What is the total number of movies and total number of comments do you have in your respective collections? The only thing I can see at this point is that your collections are missing some documents.

By the way, I like how you built lookup_pipeline with a different variable for each stage. It makes your code very clear.

Hi @Matias_Zulberti

I agree with @steevej your pipeline appears correct and it might be a missing document.

Can you search your collection to ensure it has a document with the movie_id “573a1394f29313caabcdfd61” and confirm that it is present and the name of the movie?

Thanks and kindest regards,
Eoin

Thanks @steevej and @Eoin_Brazil. I ran the following aggregation to test if I am missing either the collection “comments” or the document with the movie_id 573a1394f29313caabcdfd61.

pipeline = [
  {
    "$match": {
    "movie_id": ObjectId(id)
    },
  }
]

movie = []
for comment in db.comments.aggregate(pipeline):
    movie.append(comment)

return movie

The result was 136 documents with the id: 573a1394f29313caabcdfd61. That is the result I was expected.
I think there is something wrong here:

"let" : { "id" : '$_id'},

or here:

{ "$match" : { "$expr" : { "$eq" : ["$movie_id", "$$id"] } } }

in the way I have been using the variable I created in “let”

May be, and just may be, because I am not too familiar with python, may be the single quote around $_id are the culprit. I would try with double quotes.

That would confirm your suspicion:

Hi @Matias_Zulberti

Those look correct, the only visual thing that I can see might be the single quotes in the ‘$_id’ which you could try with double quotes “$_id”.

It could be worth a test, otherwise I’m scratching my head as it looks as it should be for the aggregation.

Kindest regards,
Eoin

>>> string = '$_id'
>>> string2 = "$_id"
>>> string
'$_id'
>>> string2
'$_id'

@steevej should be the same. I tested with double quotes and it failed again. It is so strange.
If I found the solution I will let you know what I did.

tests/test_get_comments.py::test_fetch_comments_for_movie PASSED                                                                                       [ 33%]
tests/test_get_comments.py::test_fetch_comments_for_another_movie PASSED                                                                               [ 66%]
tests/test_get_comments.py::test_comments_should_be_sorted_by_date PASSED                                                                              [100%]

Okay, now all test passed. And I will show you witch was the change I did.

This is the wrong pipeline:

pipeline = [
            {
                "$match": {
                   ...
                },
                "$lookup" : {
                   ...
                }
            }
        ]

And this is the solution:

pipeline = [
            {
                "$match": {
                    ...
                }
            },
            {
                "$lookup" : {
                    ...
                }
            }
        ]

As you can see I missed an extra pair of curly brackets. But I don’t know why I didn’t receive any error message from mongo because as I can see I created a wrong kind of pipeline.

The good notice is, I can move forward and now everything is beautiful :smiley:

2 Likes

Hi @Matias_Zulberti

That’s great to hear and delighted you are able to progress forward now!

Kindest regards,
Eoin

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.