Translate

April 15, 2013

Pymongo - TypeError: index 'password' cannot be applied to Cursor instances

In the 10Gen course I am taking of Mongo-db, we are using python to access mongo via pymongo.

I received this "TypeError : index … cannot be applied to Cursor instances" error while attempting to do the following:

user = self.users.find({'_id' : username})
...
salt = user['password'].split(',')[1]



where the "self" variable gave a connection to the mongo db, "users" was a collection on that db, and "username" was a string variable passed into a function containing this code. Initially I didn’t realize that the find() method will always return a full cursor rather than a single document, even if the where clause specifies the primary key field, _id. I’m currently working with iplbeats right now, which when retrieving a "cursor" (users) will always return a single "document" (sObect) if the primary key is supplied in the where clause.

The solution was clear: replace the find() method with the find_one() method to return a document rather than a cursor.

 

No comments: