Reputation: 3557
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!
Upvotes: 19
Views: 36055
Reputation: 558
In pymongo you can use [''] this notation to access the specific property.
Example -
cursor = collection.find({})
for document in cursor:
print document['_id']
Upvotes: 2
Reputation: 9657
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')
Upvotes: 25