简体   繁体   English

“AsyncIOMotorLatentCommandCursor”对象不可下标

[英]'AsyncIOMotorLatentCommandCursor' object is not subscriptable

I am working with motorclient and i have the following query where i want get the average rating of an certain user.我正在与 motorclient 合作,我有以下查询,我想在其中获得某个用户的平均评分。

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'])然后当我试图提取该平均值时,如print('avg rating', res['avg_rating'])

this raise an error TypeError: 'AsyncIOMotorLatentCommandCursor' object is not subscriptable这会引发错误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 .我已经用谷歌搜索了很多,但我没有找到如何使用motor从聚合查询中提取结果的解决方案。

You should await cursor or iterate through aggregate .您应该 await cursor 或遍历aggregate In addition, make changes for pipeline: it should be a list.此外,对管道进行更改:它应该是一个列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM