Yogesh Swami
Yogesh Swami

Reputation: 11

How to get only value in pymongo instead of ObjectId

I am using pymongo for my project. Everything is working fine. When I find() in my collection, I am getting:

{u'_id': ObjectId('5f08a8f62239f599569dcc6a'), u'value1': u'pizza'}

Instead of this complete field, I just want the value1. My python code is below:

import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["db_for_video"]
mycol = mydb["user"]

for x in mycol.find():
    print(x)

Can anyone help me in this?

Upvotes: 1

Views: 156

Answers (1)

deadshot
deadshot

Reputation: 9051

You can use projection

data = mycol.find({}, {'value1': 1, '_id': 0})

for x in data:
    print(x)

Output:

{'value1': 'pizza'}

Upvotes: 1

Related Questions