Project 2
Statements for Creation of DB and insertion of values to collection is attached below.
1. db.order.aggregate([
{
$lookup: {
from: "customer",
localField: "cust_id"
}
},
{
$group: {
_id: "$cust_id",
avgOrderValue: { $avg: "$ord_value" }
}
}
])
2. db.customer.aggregate([
{
$match: {
created_at: { $gte: new Date(Date.now() - 1000 * 60 * 60 * 24 * 60) }
}
},
{
$lookup: {
from: "order",
localField: "_id"
}
},
{
$sort: { ord_value: -1 }
},
{
$limit: 10
}
])
3. db.order.aggregate([
{
$match: {
created_at: {
$gte: new Date(new Date().getFullYear(), new Date().getMonth(), 1),
$lt: new Date(new Date().getFullYear(), new Date().getMonth() + 1, 1)
}
}
},
{
$group: {
_id: null,
ord_value: { $sum: "$ord_value" }
}
}
])
4. db.customer.aggregate([
{
$group: {
_id: "$city",
customerCount: { $sum: 1 }
}
},
{
$sort: {
customerCount: -1
}
}
])
5. db.sellers.aggregate([
{
$lookup: {
from: "order",
localField: "_id",
as: "orders"
}
},
{
$lookup: {
from: "customer",
localField: "orders.cust_id",
as: "customer"
}
},
{
$match: {
"customer.city": "$city"
}
},
{
$group: {
_id: "$cust_id",
totalProductsSold: { $sum: "$orders.quantity" }
}
},
{
$sort: {
totalProductsSold: -1
}
}
])