Project 2
use Ecommers,
Ans :-1
db.customers.aggregate( [
{ $unwind: "$order.last_orders" },
{ $unwind: "$order.last_orders.cart_pro" },
{
$group: {
_id: "$_id",
Avg_order_value: {$avg: "$order.last_orders.cart_pro.prod_total_amt"}
}
}
] )
Ans :-2
db.customers.aggregate( [
{$match:{"customer.resister_date":{$gte:2}}},
{ $unwind: "$order.last_orders" },
{ $unwind: "$order.last_orders.cart_pro" },
{
$group: {
_id: "$_id",
Total_order_value: {$sum: "$order.last_orders.cart_pro.prod_total_amt"}
}
},
{
$sort: { "Avg_order_value": -1 }
},
{ $limit: 10 }
] )
Ans :-3
db.sellers.aggregate([
{ $unwind: "$order_recived" },
{$group:{_id:"$order_recived.date",total_order_recived_by_date :{ $sum: 1 },
}}
])
Ans :-4
db.customers.aggregate([
{$group:{_id:"$customer.address.city",total_customer in yhis city :{ $sum: 1 }
}},
])
Ans :-5
db.sellers.aggregate( [
{$unwind:"$payments"},
{$project:{
_id:0,
"seller.fname":1,
"seller.address.city":1,
"payments.custonmer_id":1
}},
{
$lookup: {
from: "customers",
localField: "seller.address.city",
foreignField: "customer.address.city",
pipeline: [
{ $project: {
"customer.fname":1,
"customer.address.city":1}
},
],
as: "same_city_customer_and_seller"
}
},
{
$lookup: {
from: "customers",
localField: "payments.custonmer_id",
foreignField: "_id",
let: { pay_cust_id: "$payments.custonmer_id" },
pipeline: [
{ $project: {
"customer.fname":1,
"customer.address.city":1}
},
],
as: "get_order_from_same_city"
}
},
] )