I am trying to get the id from the document which I have in MongoDB, using PyMongo.
Here is my code:
docQuery = db.doctors.find({"email":doc_mail})
doc_id = docQuery[0]["_id"]["$oid"]
I have tried this too:
doc_id = docQuery[0]["_id"]
Neither of them works!
Though your second approach should work, docQuery
is a Cursor
type object. Best way is to iterate over it like:
for itm in db.doctors.find({"email":doc_mail}):
print itm.get('_id')
Or if there is only one object, then use find_one
like:
itm = db.doctors.find_one({"email":doc_mail})
print itm.get('_id')
In pymongo you can use [''] this notation to access the specific property.
Example -
cursor = collection.find({})
for document in cursor:
print document['_id']
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