I'm currently trying to have an upload function on my client side that takes a csv file and uploads it into the mongoDB. If anyone has any clue on how to help I'd really appreciate it!
Here's my current code
@router.post("/uploadfile", response_description="upload a file")
async def upload(request: Request,file: UploadFile = File(...)):
contents = file.file.read()
buffer = BytesIO(contents)
df = pd.read_csv(buffer)
buffer.close()
file.file.close()
df.to_dict(orient='records')
await request.app.mongodb["SentenceModel"].insert_one(file.file)
I also have gotten the ability for the file to upload into mongo but only as one document with all of the data as objects when it should be 1700 documents.. Here's that code
@router.post("/uploadfile", response_description="upload a file")
async def upload(request: Request,file: UploadFile = File(...)):
csvReader = csv.DictReader(codecs.iterdecode(file.file, 'utf-8'))
data = {}
for rows in csvReader:
key = rows['rowNum']
data[key] = rows
file.file.close()
await request.app.mongodb["SentenceModel"].insert_one(data)
I've tried many other methods that i've seen posted around but found no luck with any... I have no problem uploading the data but I want the data to be inserted into mongo once uploaded