Commit 7774619a by arjun.b

MongoDB task based on Aggregation

file upload api added
parent db1764cc
import csv
import json import json
from fastapi import FastAPI, UploadFile from fastapi import FastAPI, UploadFile
...@@ -19,25 +20,12 @@ async def root(): ...@@ -19,25 +20,12 @@ async def root():
return {"data": "Mongo aggregation task"} return {"data": "Mongo aggregation task"}
# # upload file from user directory @app.post("/upload", tags=["upload file"])
# @app.post("/upload", tags=["file upload"]) async def upload_file(file: UploadFile):
# async def file_upload(file: UploadFile): file_content = file.file.read()
# dataset = file.file.read() data = json.loads(file_content.decode("utf-8"))
# file_data = dataset.decode() inspection.insert_many(data)
# # print(file_data) return {"data": "CSV data uploaded successfully"}
# 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.get("/business_name with max violation issued", tags=["tasks"]) @app.get("/business_name with max violation issued", tags=["tasks"])
...@@ -45,25 +33,24 @@ async def max_violation(): ...@@ -45,25 +33,24 @@ async def max_violation():
try: try:
pipeline = [ pipeline = [
{ {
'$match': {
'result': 'Violation issued'
}
}, {
'$group': { '$group': {
'_id': '$business_name', '_id': '$business_name',
'count': { 'count': {
'$sum': 1 '$sum': 1
} }
} }
}, }, {
{ '$sort': {
'$sort':
{
'count': -1 'count': -1
} }
}, }, {
{
'$limit': 1 '$limit': 1
}, }, {
{ '$project': {
'$project':
{
'business_name': '$_id', 'business_name': '$_id',
'_id': 0 '_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