命令游标“对象不可下载 [英] command cursor' object is not subscriptable

查看:554
本文介绍了命令游标“对象不可下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python和MongoDB的新手,我正在用flask-python和MongoDB开始一个新项目。当我尝试使用 aggregate()函数返回数据时,它给了我以下错误:
$ b


command cursor'object is not subcriptable。


这是我查询的代码:

  Data = db.mytable.aggregate([{$ group:{_id:$ Name,count:{$ sum :1}}}])
return Data ['result']

我也改变了这个:

$ p $ Data = db.mytable.aggregate([{$ group:{_id:$ Name ,count:{$ sum:1}}}],userCursor = False)

但是它给了我以下错误:

lockquote
error.pymongo.errors.OperationFailure命令SON([('aggregate','mytable ''),('pipeline',[{'$ group':{'_id':'$ Name','count':{'$ sum':1}}}]),('useCursor',False)] )$ cmd失败:无法识别的字段'useCursor


注意:我正在使用MongoDB 3.2和Python3

解决方案

由于Pymongo 3.0, .aggregate() 方法返回一个 CommandCursor 没有实现 __ getitem __() 方法。相反,我建议你在函数/方法中返回游标对象,或者将结果转换成如下所示的列表: return list(Data)


I'm new in Python and MongoDB and I am starting a new project with flask-python and MongoDB. When I try returning data using the aggregate() function, it gives me the following error:

command cursor' object is not subscriptable.

Here is my code for the query:

Data = db.mytable.aggregate([ { "$group": {"_id": "$Name" ,"count": { "$sum": 1 }}}])
return Data['result']

I also changed this:

Data = db.mytable.aggregate([ { "$group": {"_id": "$Name" ,"count": { "$sum": 1 }}}],userCursor= False)

but it gives me the following error:

error.pymongo.errors.OperationFailure: command SON([('aggregate', 'mytable'), ('pipeline', [{'$group': {'_id': '$Name', 'count': {'$sum': 1}}}]), ('useCursor', False)]) on namespace mydb.$cmd failed: unrecognized field 'useCursor

Note: I am using MongoDB 3.2 and Python3

解决方案

Since Pymongo 3.0, the .aggregate() method return a CommandCursor which doesn't implement the __getitem__() method. Instead I suggest you return the cursor object in your function/method or turn to result into a list like this: return list(Data)

这篇关于命令游标“对象不可下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆