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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With