/
SwiftfeetStallion
/
Homeworks
Обзор
Документация
Войти
/
SwiftfeetStallion
/
Homeworks
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
MongoDB/scripts/task3.py
56 строк
1 KB
SwiftfeetStallion
upload files
14 апр 2025, 16:43
14 апр 2025, 16:43
cd63730
Код
Авторство
О чём код?
import pymongo client = pymongo.MongoClient("mongodb://localhost:27017/") db = client.get_database('shopsmart') customers = db.get_collection('customers') products = db.get_collection('products') orders = db.get_collection('orders') results = orders.aggregate([ { "$lookup": { "from": "products", "localField": "product_id", "foreignField": "_id", "as": "details" } }, { "$unwind": "$details" }, { "$group": { "_id": "$details.category", "total": { "$sum": {"$multiply": ["$details.price", "$quantity"]} } } } ]) print("Суммы продаж по отделам: ", results.to_list()) most_popular = products.aggregate([ { "$lookup": { "from": "orders", "localField": "_id", "foreignField": "product_id", "as": "order_details" } }, { "$unwind": "$order_details" }, { "$group": { "_id": "$name", "orders_num": { "$sum": "$order_details.quantity" } } }, { "$sort": {"orders_num": -1} }, { "$limit": 1 } ]) print("Самый популярный продукт: ", most_popular.to_list())