pymongo+MongoDB: How to find _id in pymongo?
<p>I want to find the <code>_id</code> of a document of a collection <code>(mycol)</code> where <code>"name":"John"</code>.  I have inserted the document but want to find the <code>_id</code> of document. Is it possible ? I am trying as </p>

<pre><code>result = db.mycol.find({"_id": {"name": "John"}})
</code></pre>

<p>But it is returning a cursor object.</p>

<blockquote>
  <p>pymongo.cursor.Cursor object at 0x00000000030E3DD8></p>
</blockquote>

<p>Then I tried as </p>

<pre><code>for itm in result:
            print (itm)
</code></pre>

<p>But it is not printing anything.</p>

<p>Try it like that  </p>

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

<p>Just have a look at the <a href="http://api.mongodb.com/python/current/tutorial.html#querying-for-more-than-one-document" rel="nofollow">docs</a> to see how to use pymongo</p>

python mongodb mongodb-query pymongo