Note that an ObjectId is not the same as its string representation:
>>> post_id_as_str=str(post_id)>>> posts.find_one({"_id":post_id_as_str})# No result>>>
A common task in web applications is to get an ObjectId from the request URL and find the matching document. It’s necessary in this case to convert the ObjectId from a string before passing it tofind_one:
frombson.objectidimportObjectId# The web framework gets post_id from the URL and passes it as a stringdefget(post_id):# Convert from string to ObjectId:document=client.db.collection.find_one({'_id':ObjectId(post_id)})