'AsyncIOMotorLatentCommandCursor' object is not subscriptable

2.2k Views Asked by At

I am working with motorclient and i have the following query where i want get the average rating of an certain user.

res = apps.db.rating.aggregate({
        "$group": {
            "_id": ObjectId(user_id),
            "avg_rating": {"$avg": "$rating"}
        }
    })

then while i am trying to extract that average value like print('avg rating', res['avg_rating'])

this raise an error TypeError: 'AsyncIOMotorLatentCommandCursor' object is not subscriptable

I have googled a lot already but i didn't found solution of how i can extract a resutl from an aggregate query using motor.

1

There are 1 best solutions below

0
Oleksiy On

You should await cursor or iterate through aggregate. In addition, make changes for pipeline: it should be a list.