I'm trying to do something when count() = 1. I confirmed cursor.count() == 1 by printing it out, but when I use cursor[0], it raised an exception.
if not cursor.count():
return self.create_new_incident(tweet)
elif loc_cur.count() == 1:
return self.update_existing_incident(tweet, cursor[0])
....
File "/Library/Python/2.7/site-packages/pymongo/cursor.py", line 588, in __getitem__
raise IndexError("no such item for Cursor instance")
IndexError: no such item for Cursor instance
I read this: Using pymongo's ReplicaSetConnection: sometimes getting "IndexError: no such item for Cursor" I have closed other unused connections, but still not working.
Thanks in advance
PyMongo's Cursor class uses an Iterator concept, so you should use cursor.next()
to fetch the next document.
If a cursor hasn't been created yet (eg. when you are sending the initial find()
query), you could use a index to get a single document or slice of documents in the returned cursor.