Hello, any suggestion on the best/recommended approach of doing explicit datatype conversion from Json string response (list) to MongoDB datatype ? Similar question here:- PyMongo - Python List to MongoDB datatype conversion - Stack Overflow. The api response returns json string object as a list.
Have followed this approach of converting only specific list of fields that need type cast on a copy of the list:
results = response.json() ['data']
final = [ ]
new_data = results.copy()
for i in enumerate(results):
try:
new_data[0]['date'] = datetime.strptime(i[1]['date'],'%m/%d/%Y')
new_data[0]['amount'] = float(i[1]['amount'])
new_data[0]['quantity'] = int(float(i[1]['quantity']))
new_data[0]['price'] = float(i[1]['price'])
final.append(new_data[0])
except ValueError as e:
print(e.details)
raise
#do **bulk** insert
db.insert_many(final)
However, I am getting the duplicate key error:
{'writeErrors': [{'index': 1, 'code': 11000, 'errmsg': "E11000 duplicate key error collection: testDB.testCollection index: _id_ dup key: { : ObjectId('5f4f638ace84c97b4afe81cf') }",
The length(# of records) are same in both the lists results (original) and final (post type conversion), but the list itself is not identical - may be now due to datatype conversion.
>>results==final
False
>>print(results) ##before datatype conversion
[{'name':'xyz','desc':'abcdefghijk','date': '9/1/2020', 'amount':'100','quantity':'2','price':'50'}]
>>print(final) ##after datatype conversion
[{'name':'xyz','desc':'abcdefghijk','date': datetime.datetime(2020, 9, 01, 0, 0), 'amount':100.0,'quantity':2,'price':50.0}]
I am evaluating Mongoose like PyMongo ODM wrapper called PyMODM for handling such type conversion, however doing it Pythonic way sounds more efficient - if it’s doable.
Just my two cents - regarding usage of any ORM/ODM, since MongoDB is schema-on-read; enforcing a schema/model even before loading into database collection defies the purpose of it being schema-less. Datatype conversion should still be ok for some fields doing it explicitly during pre-processing and before-loading to Mongo database.
Please recommend / suggest a solution.
Thanks!