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 SalesForce's APEX right now, which when retrieving a "cursor" (list) 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.


Comments

Add Comment

Name

Email

Comment

Are you human? * three = 3


Name: Dima

Creation Date: 2020-08-11

Thank you, that helped ;)