1

so i have a file upload functionality. after uploading the excel file, then in my python code it will read the excel file

df = pd.read_excel(file_name, target_sheet)

next im gonna do this.

json = df.to_json(orient='records', date_format='iso', date_unit='s')

then im going to update / insert the data using mongodb.

MyMongoDB.db[my_collection].update(dataType, json, upsert=True)

the dataType contains the column Header of the table. when I print the dataType it will give me this

 { u`Total: u`number, u`Agent: u`string and so on..

my plan was to avoid the data types error of all field since im extracting data from excel. when I use that one line of code. it gives me this error.

TypeError: document must be an instance of dict, bson.son.SON, or other type that inherits from collections.Mapping. 

any help is appreciated. thanks community.

Joshua Fabillar
  • 506
  • 2
  • 9
  • 24
  • Well because you exported a "JSON String" and not a `instance of dict, bson.son.SON, ...`. MongoDB data is NOT JSON. It's just inserted as a `dict` as far as python is concerned. – Neil Lunn Nov 23 '18 at 09:57
  • if I did this. json = df.to_dict() now it gives me an error of documents must have string keys. what approach should I do – Joshua Fabillar Nov 23 '18 at 11:35
  • Well that's called another problem. But it also has another answer, and of course it's based on the error message you have already. You need to fix the dict output so the keys are not numeric. – Neil Lunn Nov 23 '18 at 11:39

0 Answers0