Reputation: 11
This code gives me an error:
TypeError: 'CommandCursor' object has no attribute 'getitem'
However when my friend runs the same code on her computer, she doesn't get an error. I'm using an Intel Pentium CPU on Windows. I have MongoDB running (version 3.4), Python 2.7.13 :: Anaconda 4.3.1 (64-bit).
import json
import pymongo
from bson import json_util
import re
FROM = "[email protected]"
client = pymongo.MongoClient()
db = client.enron
mbox = db.mbox
//Get the recipient lists for each message
recipients_per_message = db.mbox.aggregate([
{"$match" : {"From" : re.compile(r".*{0}.*".format(FROM), re.IGNORECASE)}},
{"$project" : {"From" : 1, "To" : 1} },
{"$group" : {"_id" : "$From", "recipients" : {"$addToSet" : "$To" } } }
])['result'][0]['recipients']
//The exact Error was:
TypeError Traceback (most recent call last)
19 {"$project" : {"From" : 1, "To" : 1} },
20 {"$group" : {"_id" : "$From", "recipients" : {"$addToSet":"$To" }}}
---> 21 ])['result'][0]['recipients']
TypeError: 'CommandCursor' object has no attribute 'getitem'
Upvotes: 1
Views: 2826
Reputation: 24007
Your friend must be running an old version of both MongoDB and PyMongo. Since PyMongo 3.0, running "aggregate" with PyMongo returns a cursor that you must iterate for results. Try this:
results = list(db.mbox.aggregate([
{"$match" : {"From" : re.compile(r".*{0}.*".format(FROM), re.IGNORECASE)}},
{"$project" : {"From" : 1, "To" : 1} },
{"$group" : {"_id" : "$From", "recipients" : {"$addToSet" : "$To" } } }
]))
recipients_per_message = results['result'][0]['recipients']
Upvotes: 1