'AsyncIOMotorLatentCommandCursor'对象不可下标

问题描述 投票:0回答:1

我正在与motorclient一起工作,并且在下面的查询中我想获取某个用户的平均评分。

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

然后,当我尝试提取该平均值时,print('avg rating', res['avg_rating'])

这会引发错误TypeError: 'AsyncIOMotorLatentCommandCursor' object is not subscriptable

我已经用谷歌搜索了很多,但是我没有找到如何使用motor从聚合查询中提取结果的解决方案。

python mongodb sanic motorengine
1个回答
0
投票

您应该等待光标或通过aggregate进行迭代。另外,对管道进行更改:它应该是一个列表。

© www.soinside.com 2019 - 2024. All rights reserved.