0

I'm using Swagger+pymongo and I wanted something really simple to convert a string into an ObjectId. How can I do this very easily without touching other db schemas.(Minimal effort)?

Code:

jsonResponse = request.json['business']
# convert business_id Datatype to ObjectId
business_id=ObjectId(jsonResponse['business_id'])

#add business_id (ObjectId)to mongodb
data = collection.insert_one(jsonResponse).inserted_id
return data

1 Answer 1

1
response = request.json['business']
response_oid = ObjectId(response['business_id'])

mongo_item = response.copy()
mongo_item['business_id'] = response_oid

return collection.insert_one(mongo_item).inserted_id

Should do the job.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.