ERROR: assert comments[0].get('_id') == result.inserted_id

Please Help!
It’s very confusing!
I did everything was needed but I get error after running: pytest -m create_update_comments

Most likely you are not sorting the comments correctly in get_movie().

This is my pipeline within get_movie(), but doesn’t work:

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

When using pipeline: you must specify the query differently.

See and search for pipeline: to see the syntax you need.

I read the content of the link you mentioned and changed my pipeline that way. But unfortunately there is the same error message. In addition, both the new and previous pipelines in the MongoDBCompass program work well and display the results correctly!
New pipeline:

pipeline = [ {'$match': {'_id': ObjectId(id)}}, {'$lookup': { 'from': 'comments', 'let': {'id': '$_id'}, 'pipeline': [ {'$match': {'$expr': {'$eq': ['$movie_id', '$$id']}}}, {'$sort': {'date': -1}} ], 'as': 'comments' } } ]

and this is my add_comment():


The new pipeline seems ok. If the old worked in Compass then you must have been connected to a 5.0+ server.

In you add_comment I would use "movie_id" : ObjectId( movie_id ) in case the API is giving the string representation of the movie_id.


Yes, It’s worked finally!
Thank you very much Steeve :clap:
The last point you mentioned was the key to this puzzle!

1 Like

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