You need to convert _id
variable (that's of type str
) to ObjectId
type first
from bson.objectid import ObjectId
if type(_id) is not ObjectId:
_id = ObjectId(_id)
Take a look: http://api.mongodb.org/python/2.7rc0/tutorial.html#querying-by-objectid