Skip to content

Commit

Permalink
completed
Browse files Browse the repository at this point in the history
  • Loading branch information
justintan2002 committed Oct 31, 2021
1 parent fc973f7 commit 6f72654
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
db.py
Binary file added __pycache__/db.cpython-39.pyc
Binary file not shown.
42 changes: 41 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 2 additions & 0 deletions tempCodeRunnerFile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

{"$addFields": {"price_per_book": {"$divide": [int("$total_price"), int("$copies_sold"

0 comments on commit 6f72654

Please sign in to comment.