title: Removing _id element from Pymongo results
body: <p>I'm attempting to create a web service using MongoDB and Flask (using the pymongo driver).  A query to the database returns documents with the "_id" field included, of course.  I don't want to send this to the client, so how do I remove it?</p>
  
  <p>Here's a Flask route:</p>
  
  <pre><code>@app.route('/theobjects')
  def index():
      objects = db.collection.find()
      return str(json.dumps({'results': list(objects)}, 
          default = json_util.default,
          indent = 4))
  </code></pre>
  
  <p>This returns: </p>
  
  <pre><code>{
  "results": [
      {
          "whatever": {
              "field1": "value", 
              "field2": "value", 
          }, 
          "whatever2": {
              "field3": "value"
          },
          ...
          "_id": {
              "$oid": "..."
          }, 
  
      ...
      }
  ]}
  </code></pre>
  
  <p>I thought it was a dictionary and I could just delete the element before returning it:</p>
  
  <pre><code>del objects['_id']
  </code></pre>
  
  <p>But that returns a TypeError:</p>
  
  <pre><code>TypeError: 'Cursor' object does not support item deletion
  </code></pre>
  
  <p>So it isn't a dictionary, but something I have to iterate over with each result as a dictionary.  So I try to do that with this code:</p>
  
  <pre><code>for object in objects:
      del object['_id']
  </code></pre>
  
  <p>Each object dictionary looks the way I'd like it to now, but the objects cursor is empty.  So I try to create a new dictionary and after deleting _id from each, add to a new dictionary that Flask will return:</p>
  
  <pre><code>new_object = {}
  for object in objects:
      for key, item in objects.items():
          if key == '_id':
              del object['_id']
              new_object.update(object)
  </code></pre>
  
  <p>This just returns a dictionary with the first-level keys and nothing else.  </p>
  
  <p>So this is sort of a standard nested dictionaries problem, but I'm also shocked that MongoDB doesn't have a way to easily deal with this.</p>
  
  <p>The <a href="http://www.mongodb.org/display/DOCS/Retrieving+a+Subset+of+Fields" rel="noreferrer">MongoDB documentation</a> explains that you can exclude _id with</p>
  
  <pre><code>{ _id : 0 }
  </code></pre>
  
  <p>But that does nothing with pymongo.  The <a href="http://api.mongodb.org/python/current/api/pymongo/collection.html#pymongo.collection.Collection.find" rel="noreferrer">Pymongo documentation</a> explains that you can list the fields you want returned, but "(“_id” will always be included)".  Seriously?  Is there no way around this?  Is there something simple and stupid that I'm overlooking here? </p>