user3787092
user3787092

Reputation: 71

pymongo+MongoDB: How to find _id in pymongo?

I want to find the _id of a document of a collection (mycol) where "name":"John". I have inserted the document but want to find the _id of document. Is it possible ? I am trying as

result = db.mycol.find({"_id": {"name": "John"}})

But it is returning a cursor object.

pymongo.cursor.Cursor object at 0x00000000030E3DD8>

Then I tried as

for itm in result:
            print (itm)

But it is not printing anything.

Upvotes: 2

Views: 3992

Answers (1)

DAXaholic
DAXaholic

Reputation: 35338

Try it like that

result = db.mycol.find({"name": "John"})
for item in result:
    print(item['_id'])

Just have a look at the docs to see how to use pymongo

Upvotes: 1

Related Questions