#Querying Mongodb

3 messages · Page 1 of 1 (latest)

gaunt hound
#

Okay. I have been able to do this using pymongo and then celery to sync postgres directly to mongodb such that as soon as an instance is saved on postgres, it automatically syncs this change on mongodb. However I am having a challenge now. I want to write a task that calls an api from mongodb using celery and then write an endpoint for it in python. When I did, I got this error:

    trending_movies = movie_collection.find({'status': 'running'}).sort('ranking', -1).limit(10)
AttributeError: 'list' object has no attribute 'find'.```

This is the task:

```@shared_task
def get_trending_movies() -> List:
    try:
        update_movie_rank.delay() # call update_movie_rank as a dependency
        movie_collection = mongo_client[mongo_db_name]['movies']
        trending_movies = list(movie_collection.find({'status': 'running'}).sort('ranking', -1).limit(10))
        return trending_movies
    except PyMongoError as e:
        raise HTTPException(status_code=500, detail=str(e))

And this is the endpoint:

def list_trending_movies(request):
    try:
        movie_collection = get_trending_movies()
        trending_movies = movie_collection.find({'status': 'running'}).sort('ranking', -1).limit(10)
        return trending_movies
    except PyMongoError as e:
        return HTTPException(status_code=500, detail=str(e))```
whole reef
#

get_trending_movies is returning a list and then you're trying to execute a find query on it.
surely you should be doing trending_movies = get_trending_movies() in list_trending_movies ?

gaunt hound