Removing _id element from Pymongo results

Question:

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?

Here’s a Flask route:

@app.route('/theobjects')
def index():
    objects = db.collection.find()
    return str(json.dumps({'results': list(objects)}, 
        default = json_util.default,
        indent = 4))

This returns:

{
"results": [
    {
        "whatever": {
            "field1": "value", 
            "field2": "value", 
        }, 
        "whatever2": {
            "field3": "value"
        },
        ...
        "_id": {
            "$oid": "..."
        }, 

    ...
    }
]}

I thought it was a dictionary and I could just delete the element before returning it:

del objects['_id']

But that returns a TypeError:

TypeError: 'Cursor' object does not support item deletion

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:

for object in objects:
    del object['_id']

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:

new_object = {}
for object in objects:
    for key, item in objects.items():
        if key == '_id':
            del object['_id']
            new_object.update(object)

This just returns a dictionary with the first-level keys and nothing else.

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.

The MongoDB documentation explains that you can exclude _id with

{ _id : 0 }

But that does nothing with pymongo. The Pymongo documentation 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?

Asked By: ddw

||

Answers:

You are calling

del objects['_id']

on the cursor object!

The cursor object is obviously an iterable over the result set and not single
document that you can manipulate.

for obj in objects:
     del obj['_id']

is likely what you want.

So your claim is completely wrong as the following code shows:

import pymongo

c = pymongo.Connection()
db = c['mydb']
db.foo.remove({})
db.foo.save({'foo' : 42})

for row in db.foo.find():
    del row['_id']
    print row



$ bin/python foo.py 

> {u'foo': 42}
Answered By: Andreas Jung

To exclude the _id field in a find query in pymongo, you can use:

db.collection.find({}, {'_id': False})

The documentation is somewhat missleading on this as it says the _id field is always included. But you can exclude it like shown above.

Answered By: Thomas

Above answer fails if we want specific fields and still ignore _id. Use the following in such cases:

db.collection.find({'required_column_A':1,'required_col_B':1, '_id': False})
Answered By: srikar saggurthi
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.