Reputation:
I want to fetch document from mongoDB using python i am able to fetch all attribute but i just wanted to fetch its object id and store it into a variable .
i am trying following code
from pymongo import Connection
from bson.objectid import ObjectId
connection = Connection()
server="localhost"
port = 27017
connection =Connection(server , port)
db = connection.flipkart
connection = db.flipitems
connection.remove({'name':None})
for post in connection.find():
print post['_id':'ObjectId']
seller_name = post['seller_name']
name = post['name']
but it gives following error
shubham@shubham-pc:~/Desktop/python$ python fetch_data.py Traceback
(most recent call last): File "fetch_data.py", line 18, in
print post['_id':'ObjectId'] TypeError: unhashable type
Upvotes: 3
Views: 1653
Reputation: 252
You can simply use
print str(post['_id'])
This will give the ObjectId in string format
print post['_id']
will return the objectId as ObjectId('123456789023456') itself
Upvotes: 2
Reputation: 12077
You are wrong in post['_id':'ObjectId']
, you should do either:
print post['_id']
or
print post['ObjectId']
Upvotes: 0