Diolor
Diolor

Reputation: 13450

Pymongo return data without _id

As the title says, how can I find() in my mongo all the data but without the _id index in the list.

Of course I can drop it later like:

for i in data:
   del i['_id']

But is there a more elegant solution without needing that loop?

Upvotes: 3

Views: 2573

Answers (2)

Chris Chang
Chris Chang

Reputation: 426

If I understand your question correctly, I think you're looking for projections:

http://docs.mongodb.org/manual/reference/method/db.collection.find/#projections

Something like this should do it:

db.test.find(query,{"_id":0})

Upvotes: 5

Paco
Paco

Reputation: 4708

Something like ?

data = [dict(zip([f for f in e if '_id' not in f], e.values())) for e in data]

Although, I wouldn't use it, as it is not clear at all

Upvotes: 0

Related Questions