I am using pymongo driver. Supposedly, one can use a string to query the _id field of a document, like this:
thing = db.things.find_one({'_id':'4ea113d6b684853c8e000001'})
But it doesn't work. What am I doing wrong?
Yes, you can use a string as your _id.
Although there are other drivers written by the community, PyMongo is the official Python driver for MongoDB.
It should be :
from pymongo.objectid import ObjectId thing = db.things.find_one({'_id': ObjectId('4ea113d6b684853c8e000001') })
EDIT: The current import is: from bson.objectid import ObjectId
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With