0

When i run this code ,i get an error 'CommandCursor' object is not subscriptable pymongo version = 3.3.0

try:
    myresults = students.aggregate([{"$unwind":"$scores"},{"$match":{"scores.type":"homework"}},{"$group":{"_id":"$_id","minitem":{"$min":"$scores.score"}}}],useCursor=False)
    for result in myresults['result']:
        scores.update({"_id":result["_id"]},{"$pull":{"scores":{"score": result["minitem"]}}})
except Exception as e:
    raise
Sarthak Gupta
  • 824
  • 12
  • 23

2 Answers2

0

i removed the result from myresult['result'] and it worked

try:
    myresults = students.aggregate([{"$unwind":"$scores"},{"$match":{"scores.type":"homework"}},{"$group":{"_id":"$_id","minitem":{"$min":"$scores.score"}}}],useCursor=False)
    for result in myresults:
        students.update({"_id":result["_id"]},{"$pull":{"scores":{"score": result["minitem"]}}})
except Exception as e:
    raise
Alp
  • 29,274
  • 27
  • 120
  • 198
Sarthak Gupta
  • 824
  • 12
  • 23
0

The aggregate query returns a cursor when useCursor is True, otherwise list. So you should be processing aggregate result by looping through the result.

daemon24
  • 1,388
  • 1
  • 11
  • 23