Commit 7774619a by arjun.b

MongoDB task based on Aggregation

file upload api added
parent db1764cc
import csv
import json
from fastapi import FastAPI, UploadFile
......@@ -19,25 +20,12 @@ async def root():
return {"data": "Mongo aggregation task"}
# # upload file from user directory
# @app.post("/upload", tags=["file upload"])
# async def file_upload(file: UploadFile):
# dataset = file.file.read()
# file_data = dataset.decode()
# # print(file_data)
# return {"file": file}
# insert json data into database
@app.post("/insert_json", tags=["insert JSON"])
async def insert_data():
try:
with open(file_path) as data:
file_data = json.load(data)
inspection.insert_many(file_data)
return {"data": "JSON data successfully inserted"}
except Exception as e:
print(str(e))
@app.post("/upload", tags=["upload file"])
async def upload_file(file: UploadFile):
file_content = file.file.read()
data = json.loads(file_content.decode("utf-8"))
inspection.insert_many(data)
return {"data": "CSV data uploaded successfully"}
@app.get("/business_name with max violation issued", tags=["tasks"])
......@@ -45,25 +33,24 @@ async def max_violation():
try:
pipeline = [
{
'$match': {
'result': 'Violation issued'
}
}, {
'$group': {
'_id': '$business_name',
'count': {
'$sum': 1
}
}
},
{
'$sort':
{
}, {
'$sort': {
'count': -1
}
},
{
}, {
'$limit': 1
},
{
'$project':
{
}, {
'$project': {
'business_name': '$_id',
'_id': 0
}
......
No preview for this file type
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment