Project 2
Q1. Write a query to find the average order value for all Customers.
Answer :-
db.customer.aggegate([
{$group:{_id:"cust_id", average_orders:{$avg:"$order_value"}}}
])
Q2. Query your database to get top 10 customers whose orders have the highest value and who have been customers since less than 2 months.
Answer :-
db.order.aggregate([
{$match:{ord_date:{$lt:"2022-10-01"}}},
{$sort:{order_value:-1}},{$limit:(10)}
])
Q3. Query your database to find the sum of all orders received in this month.
Answer :-
db.order.aggregate([
{$match:{order_date:{$gte:"2023-01-01", $lte:"2023-01-31"}}},
{$group:{_id:"cust_id", orders:{$sum:1}}}
])
Q4. Query your database to find customers count in various cities, group them by names of cities.
Answer :-
db.customer.aggregate([
{$group:{_id:"$city"}},{$count:"city"}
])
Q5. Query your database to get all the sellers who have base in the same city in which the customers who ordered their products. Order them in the order of highest number of products sold to customers in same city to lowest.
Answer :-
db.getCollection("seller").aggregate([
{
$lookup:{
from:"customer",
localField:"city",
foreignField:"city",
as:"customer_details"
}
},{$sort:{product_sold:-1}},{$match:{"customer_details":{$ne:[]}}}
])