Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

command cursor' object is not subscriptable

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

like image 866
Kaushik Makwana Avatar asked Mar 13 '23 15:03

Kaushik Makwana


1 Answers

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)

like image 171
styvane Avatar answered Mar 16 '23 03:03

styvane