Please take a look at the following code:
collection = db_name.get_db().collection_name
print collection
# prints .. Collection(Database(Connection('localhost', 27017), u'db_name'), u'colelction_name')
for key in some_dict.keys():
query = {"p_id":key}
document = collection.find(query)
print document
# gives <pymongo.cursor.Cursor object at 0x7f13f3049b10>
Now I want to retreive this document.. and fetch the data.
but if i do:
for d in document:
print d
I get the following error
File "/usr/local/lib/python2.7/dist-packages/pymongo/cursor.py", line 703, in next
if len(self.__data) or self._refresh():
File "/usr/local/lib/python2.7/dist-packages/pymongo/cursor.py", line 666, in _refresh
self.__uuid_subtype))
File "/usr/local/lib/python2.7/dist-packages/pymongo/cursor.py", line 628, in __send_message
self.__tz_aware)
File "/usr/local/lib/python2.7/dist-packages/pymongo/helpers.py", line 101, in _unpack_response
error_object["$err"])
What am i doing wrong?
Thanks
You want to retrieve “this document” – then use the official method for fetching one document matching your criteria: find_one():
http://api.mongodb.org/python/2.2/api/pymongo/collection.html
Reading basic API documentation is your friend.