Ticket: Get Comments. What is difference?(failed unit test. but I got validation code, anyway )

I checked late for the hint(sort by comments date while lookup) of the problem and solved it by unwind by comments, then sorting, then group by _id with push comments.

I got validation code but unit test fails as belows.

  client = <FlaskClient <Flask 'mflix.factory'>>

def test_comments_should_be_sorted_by_date(client):
    # in order from most to least recent
    movie_ids = [
    for id in movie_ids:
>           comments = get_movie(id).get('comments', [])
E           AttributeError: 'NoneType' object has no attribute 'get'
tests/test_get_comments.py:41: AttributeError

I don’t know the difference between lookup in pymongo and mongodb’s aggregation operator.

Below is my pipeline code.

    pipeline = [
        {"$match": {"_id": ObjectId(id)}},
        {'$lookup': {'from': 'comments', 'localField': '_id', 'foreignField': 'movie_id', 'as': 'comments'}},
        {'$unwind': '$comments'},
        {'$sort': {'comments.date': -1}},
        {'$group': {'_id': '$_id', 'comments': {'$push': '$comments'}}},

Hi @ys_70859,

You need to check the code and try to use sort in lookup pipeline only, otherwise the results might differ.

This link may help: