From 6f726542925d57388114df2935abdb578a3912b0 Mon Sep 17 00:00:00 2001 From: Justin Tan Date: Mon, 1 Nov 2021 04:35:10 +0800 Subject: [PATCH] completed --- .gitignore | 1 + __pycache__/db.cpython-39.pyc | Bin 0 -> 356 bytes app.py | 42 +++++++++++++++++++++++++++++++++- tempCodeRunnerFile.py | 2 ++ 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 __pycache__/db.cpython-39.pyc create mode 100644 tempCodeRunnerFile.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5911997 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +db.py diff --git a/__pycache__/db.cpython-39.pyc b/__pycache__/db.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..fc6e4c0717ff7e05638c57143b1fc01f5a228c73 GIT binary patch literal 356 zcmYjN!A`7VFz4Nwy?X@6oQ}OKllm$ zO0WKfC%43dlgyiWGn2`iL6yp(BJJes9ev37os<8jO45?fk0oHmSFq}8fp^#)9&=NFfomqkoxF&>|BmNaU$?XI!iwb(0$gd1Zp_FZ#RPv?pGH)&E3 zH33}Qa2_=UTf?bL!in4%GFb6u$95k>XW-aQwU`}Q?a&+9u5;VBvyvpkXDcgvZZGWJ zIS-!-)1=fr{46Goz~jJpFyG4z1{5qt63`9ZL4w2-MY>02fpH>IJ)Y_#LT6|hGYDKX zokqa*E{dYS?MnR;E&Iu6+eY2!=SL375+Xt&z+-cnX636zjMoud=U?Nw3{BP4U)%d? A(*OVf literal 0 HcmV?d00001 diff --git a/app.py b/app.py index e293225..a668fb3 100644 --- a/app.py +++ b/app.py @@ -1 +1,41 @@ -# Your DB Code Here +from db import DB_USERNAME, DB_PWD, URL_LINK +import pymongo +URL = URL_LINK.format( + DB_USERNAME, DB_PWD) +print(URL) +client = pymongo.MongoClient(URL) +db = client["Bookstore"] + +pipeline = [{"$group": {"_id": "$isbn", "number_of_entries": {"$sum":1}}}] +result = db.books_selling_data.aggregate(pipeline) +#for books in result: +# print(books) + +pipeline = [{"$group": {"_id": "$isbn", "total_copies_sold": {"$sum":"$copies_sold"}}}, + {"$match": {"total_copies_sold":{"$gt":15000}}}, + {"$lookup": {"from": "books", "localField": "_id", "foreignField": "isbn", "as": "details"}}, + {"$project": {"_id":0, "details":{"title": 1}}}] +result = db.books_selling_data.aggregate(pipeline) +#for books in result: +# print(books) + +pipeline = [{"$match": {"author": "John Doe"}}, + {"$sort": {"isbn": 1}}, + {"$project": {"_id": 0, "isbn": 0}}] +result = db.books.aggregate(pipeline) +#for books in result: +# print(books) + +pipeline = [{"$addFields": {"price_per_book": {"$round": [{"$divide": [{"$toInt": "$total_price"}, {"$toInt": "$copies_sold"}]}, 2]}}}, + {"$project": {"_id":0, 'copies_sold': 0, 'total_price': 0, 'supplier': 0}}] +result = db.books_selling_data.aggregate(pipeline) +#for books in result: +# print(books) + +pipeline = [{"$unwind": "$supplier"}, + {"$match": {"supplier": "titan"}}, + {"$addFields": {"price_per_book": {"$round": [{"$divide": [{"$toInt": "$total_price"}, {"$toInt": "$copies_sold"}]}, 2]}}}, + {"$project": {"_id":0, 'copies_sold': 0, 'total_price': 0, 'supplier': 0}}] +result = db.books_selling_data.aggregate(pipeline) +#for books in result: +# print(books) diff --git a/tempCodeRunnerFile.py b/tempCodeRunnerFile.py new file mode 100644 index 0000000..67a17ee --- /dev/null +++ b/tempCodeRunnerFile.py @@ -0,0 +1,2 @@ + + {"$addFields": {"price_per_book": {"$divide": [int("$total_price"), int("$copies_sold" \ No newline at end of file